Librería os (Interfaces al sistema operativo)#

  • Última modificación: Mayo 14, 2022

[1]:
import os
[2]:
# -----------------------------------------------------------------------------
# getcwd(): get current working directory
#
cwd = os.getcwd()
cwd
[2]:
'/workspace/python_advanced'
[3]:
# -----------------------------------------------------------------------------
# chdir(path): Change the current working directory to path.
#
os.chdir('/tmp')
os.getcwd()
[3]:
'/tmp'
[4]:
os.chdir(cwd)
os.getcwd()
[4]:
'/workspace/python_advanced'
[5]:
# -----------------------------------------------------------------------------
# listdir(path='.'): Return a list containing the names of the entries in the directory given by path.
#
os.listdir()
[5]:
['.DS_Store', '.ipynb_checkpoints', '1-02_os.ipynb']
[6]:
os.listdir("/root")
[6]:
['.profile', '.bashrc', '.local', '.jupyter', '.ipython', '.cache']
[7]:
# -----------------------------------------------------------------------------
# mkdir(path): Create a directory named path.
#
os.mkdir('./demo')
!ls -1
1-02_os.ipynb
demo
[8]:
# -----------------------------------------------------------------------------
# makedirs(name): Recursive directory creation function.
#
os.makedirs('./branch-0/branch-1/branch-2')
[9]:
# -----------------------------------------------------------------------------
# remove(path): Remove (delete) the file path. If path is a directory, an IsADirectoryError is raised.
#
!touch delete.me
!ls
1-02_os.ipynb  branch-0  delete.me  demo
[10]:
os.remove('delete.me')
!ls
1-02_os.ipynb  branch-0  demo
[11]:
# -----------------------------------------------------------------------------
# Remove (delete) the empty directory path.
#
os.rmdir("demo")
!ls -1
1-02_os.ipynb
branch-0
[12]:
# -----------------------------------------------------------------------------
# removedirs(name)
#
os.removedirs('branch-0/branch-1/branch-2')
!ls -1
1-02_os.ipynb
[13]:
# -----------------------------------------------------------------------------
# rename(src, dst): Rename the file or directory src to dst.  Fails if dst exists!
#
!touch rename.me
!ls -1
1-02_os.ipynb
rename.me
[14]:
os.rename('rename.me', 'renamed')
!ls -1
1-02_os.ipynb
renamed
[15]:
# -----------------------------------------------------------------------------
# replace(src, dst): Rename the file or directory src to dst.
#
!echo "content of rename.me" > rename.me
!echo "content of renamed" > renamed
!ls -1
1-02_os.ipynb
rename.me
renamed
[16]:
os.replace('rename.me', 'renamed')
!ls -1
1-02_os.ipynb
renamed
[17]:
!cat renamed
content of rename.me
[18]:
# -----------------------------------------------------------------------------
# rmdir(path): Remove (delete) the empty directory path.
#
os.makedirs('./branch-0/branch-1/branch-2')
!ls -1
1-02_os.ipynb
branch-0
renamed
[19]:
# -----------------------------------------------------------------------------
# environ: retorna las variables de ambiente
#
os.environ
[19]:
environ{'HOSTNAME': '944a418c8e87',
        'PWD': '/workspace',
        'HOME': '/root',
        'LANG': 'C.UTF-8',
        'TERM': 'xterm-color',
        'SHLVL': '0',
        'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
        'DEBIAN_FRONTEND': 'dialog',
        '_': '/usr/local/bin/jupyter',
        'PYDEVD_USE_FRAME_EVAL': 'NO',
        'JPY_PARENT_PID': '1',
        'CLICOLOR': '1',
        'PAGER': 'cat',
        'GIT_PAGER': 'cat',
        'MPLBACKEND': 'module://matplotlib_inline.backend_inline'}
[20]:
os.environ["HOME"]
[20]:
'/root'
[21]:
# -----------------------------------------------------------------------------
# system: Execute the command (a string) in a subshell.
#
os.system("ls /root")
[21]:
0
[25]:
!rm -rf branch-0 renamed
[22]:
#
# Uso de glob para especificar wildcards
#
# import glob

# glob.glob("*.ipynb")
[23]:
#
# Ejecución de comandos y captura del retvalue
#
# os.system("ls /root")
[24]:
import subprocess

# output = subprocess.check_output(["ls", "-1"])
# output = output.decode("UTF-8")
# print(output)