os Module in Python

os Module in Python

This module provides a unified interface to a number of operating system functions. We have the following methods for the os module:

>>> import os
>>> dir(os)
[‘F_OK’, ‘MutableMapping’, ‘O_APPEND’, ‘O_BINARY’, ‘O_CREAT’, ‘O_EXCL’, ‘O_NOINH
OK’, ‘X_OK’, ‘_Environ’, ‘__all__’, ‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__
file__’, ‘__initializing__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘_copyreg’
, ‘_execvpe’, ‘_exists’, ‘_exit’, ‘_get_exports_list’, ‘_get_masked_mode’, ‘_mak
e_stat_result’, ‘_make_statvfs_result’, ‘_pickle_stat_result’, ‘_pickle_statvfs_
result’, ‘_putenv’, ‘_unsetenv’, ‘_wrap_close’, ‘abort’, ‘access’, ‘altsep’, ‘ch
dir’, ‘chmod’, ‘close’, ‘closerange’, ‘curdir’, ‘defpath’, ‘device_encoding’, ‘d
evnull’, ‘dup’, ‘dup2’, ‘environ’, ‘errno’, ‘error’, ‘execl’, ‘execle’, ‘execlp’
, ‘execlpe’, ‘execv’, ‘execve’, ‘execvp’, ‘execvpe’, ‘extsep’, ‘fdopen’, ‘fsdeco
de’, ‘fsencode’, ‘fstat’, ‘fsync’, ‘get_exec_path’, ‘get_terminal_size’, ‘getcwd
‘, ‘getcwdb’, ‘getenv’, ‘getlogin’, ‘getpid’, ‘getppid’, ‘isatty’, ‘kill’, ‘line
sep’, ‘link’, ‘listdir’, ‘lseek’, ‘lstat’, ‘makedirs’, ‘mkdir’, ‘name’, ‘open’,
‘pardir’, ‘path’, ‘pathsep’, ‘pipe’, ‘popen’, ‘putenv’, ‘read’, ‘readlink’, ‘rem
ove’, ‘removedirs’, ‘rename’, ‘renames’, ‘replace’, ‘rmdir’, ‘sep’, ‘spawnl’, ‘s
pawnle’, ‘spawnv’, ‘spawnve’, ‘st’, ‘startfile’, ‘stat’, ‘stat_float_times’, ‘st
at_result’, ‘statvfs_result’, ‘strerror’, ‘supports_bytes_environ’, ‘supports_di
r_fd’, ‘supports_effective_ids’, ‘supports_fd’, ‘supports_follow_symlinks’, ‘sym
link’, ‘sys’, ‘system’, ‘terminal_size’, ‘times’, ‘times_result’, ‘umask’, ‘unam
e_result’, ‘unlink’, ‘urandom’, ‘utime’, ‘waitpid’, ‘walk’, ‘write’]

A lot of functions! They are really useful, especially for operating system administrators.

>>> os.getcwd()

We can check the folder we are currently.

>>> os.chdir(“scripts”)
>>> os.getcwd()

We can change the folder we are currently.

>>> os.listdir(“D:/Python33”)
[‘DLLs’, ‘Doc’, ‘include’, ‘Lib’, ‘libs’, ‘LICENSE.txt’, ‘NEWS.txt’, ‘python.exe
‘, ‘pythonw.exe’, ‘pywin32-wininst.log’, ‘README.txt’, ‘Removepywin32.exe’, ‘scr
ipts’, ‘tcl’, ‘Tools’, ‘w9xpopen.exe’, ‘__pycache__’]

We can see the content of our folder.

>>> os.name

We can get the name of our operating system we are working with.

>>> for e in os.walk(“D:/Python33/scripts”):
…     print(e)

(‘D:/Python33/scripts’, [‘Phone’, ‘Systemy’, ‘__pycache__’], [‘docs.py’, ‘lista.
py’, ‘lista1.py’, ‘lista2.py’, ‘prostokat.py’, ‘prostokat1.py’, ‘trt.py’, ‘zwier
(‘D:/Python33/scripts\\Phone’, [], [‘Dsl.py’, ‘G2.py’, ‘G3.py’, ‘Isdn.py’, ‘Pots
.py’, ‘__Init__.py’])
(‘D:/Python33/scripts\\Systemy’, [‘Bazy’, ‘Lang’, ‘__pycache__’], [‘Lin1.py’, ‘L
in2.py’, ‘Win1.py’, ‘Win2.py’, ‘Win3.py’, ‘Win4.py’, ‘__init__.py’])
(‘D:/Python33/scripts\\Systemy\\Bazy’, [‘__pycache__’], [‘baz1.py’, ‘baz2.py’, ‘
baz3.py’, ‘__init__.py’])
(‘D:/Python33/scripts\\Systemy\\Bazy\\__pycache__’, [], [‘baz1.cpython-33.pyc’,
‘baz2.cpython-33.pyc’, ‘baz3.cpython-33.pyc’, ‘__init__.cpython-33.pyc’])
(‘D:/Python33/scripts\\Systemy\\Lang’, [‘Nets’, ‘__pycache__’], [‘Jez1.py’, ‘Jez
2.py’, ‘Jez3.py’, ‘Jez4.py’, ‘__init__.py’])
(‘D:/Python33/scripts\\Systemy\\Lang\\Nets’, [‘__pycache__’], [‘Net1.py’, ‘Net2.
py’, ‘Net3.py’, ‘__init__.py’])
(‘D:/Python33/scripts\\Systemy\\Lang\\Nets\\__pycache__’, [], [‘Net1.cpython-33.
pyc’, ‘Net2.cpython-33.pyc’, ‘Net3.cpython-33.pyc’, ‘__init__.cpython-33.pyc’])
(‘D:/Python33/scripts\\Systemy\\Lang\\__pycache__’, [], [‘Jez1.cpython-33.pyc’,
‘Jez2.cpython-33.pyc’, ‘Jez3.cpython-33.pyc’, ‘Jez4.cpython-33.pyc’, ‘__init__.c
(‘D:/Python33/scripts\\Systemy\\__pycache__’, [], [‘Lin1.cpython-33.pyc’, ‘Lin2.
cpython-33.pyc’, ‘Win1.cpython-33.pyc’, ‘Win2.cpython-33.pyc’, ‘Win3.cpython-33.
pyc’, ‘Win4.cpython-33.pyc’, ‘__init__.cpython-33.pyc’])
(‘D:/Python33/scripts\\__pycache__’, [], [‘lista.cpython-33.pyc’, ‘lista2.cpytho
n-33.pyc’, ‘prostokat.cpython-33.pyc’, ‘zwierz.cpython-33.pyc’])

We can walks through our directory tree and we get the path, the list of our directories, and the list of our files.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s