Python Notes

The os module

The os module allows to interact with the operating system — recognize/change the file structure, environment variables, find out the name and permissions of the user, etc. A program that uses the variables and functions of the os module is portable from one operating system to another, because os can take into account the characteristics of each OS . However, a number of functions are used only for Unix-like operating systems.

It should be noted that some of the functionality of the os module is implemented by other Python modules and built-in functions. In this case, it is often better to choose them. For example, the os.access() function checks for file access. If the file is opened for reading or writing, it is easier to use the open() function, and in case of access denial, to handle the PermissionError exception that has occurred.

Below are some of the os module methods for working with the file system.

The checking of current directory:

>>> os.getcwd()
'/home/pl/Documents/python'

Create directory:

>>> os.mkdir('TEXT')

Create a directory tree:

>>> os.makedirs('ONE/TWO/THREE')
>>> os.listdir('ONE')
['TWO']
>>> os.listdir('ONE/TWO')
['THREE']

Directory listing (non-recursive):

>>> os.listdir()
['text4.txt', 'text1.txt', 'text3.txt', 'try_except.py', 'files.py', 'text5.txt', 'grades_oop.py', 'grades.py', 'text6.txt', 'text2.txt', 'text.txt', 'TEXT']
>>> os.listdir('/home')
['pl']

File | directory information:

>>> os.stat('/home')
posix.stat_result(st_mode=16877, st_ino=1048577, st_dev=2053, st_nlink=3, st_uid=0, st_gid=0, st_size=4096, st_atime=1344368518, st_mtime=1339316982, st_ctime=1339316982)
>>> os.stat('text.txt')
posix.stat_result(st_mode=33188, st_ino=1575740, st_dev=2053, st_nlink=1, st_uid=1000, st_gid=1000, st_size=41, st_atime=1344124896, st_mtime=1343895362, st_ctime=1343895362)

Rename file | directory:

>>> os.rename('text2.txt', 'xett.txt')
>>> os.rename('TEXT', 'ETXT')

Delete directory:

>>> os.rmdir('ETXT')

Change the current directory:

>>> os.chdir('/home')
>>> os.getcwd()
'/home'
>>> os.chdir('./pl/Documents/python')
>>> os.getcwd()
'/home/pl/Documents/python'

The os module should be preferred when working with individual files, the shutil module, which allows performing operations on data trees, is more suitable for working with a group of files.

Copy tree:

>>> shutil.copytree('../python', '../copy-python')
>>> os.listdir('../copy-python')
['text4.txt', 'text1.txt', 'text3.txt', 'try_except.py', 'ONE', 'files.py', 'xett.txt', 'text5.txt', 'grades_oop.py', 'grades.py', 'text6.txt', 'text.txt']

Move | rename tree:

>>> shutil.move('../copy-python', 'python-copy')
>>> os.listdir()
['text4.txt', 'text1.txt', 'text3.txt', 'try_except.py', 'ONE', 'files.py', 'xett.txt', 'text5.txt', 'grades_oop.py', 'python-copy', 'grades.py', 'text6.txt', 'text.txt']

Deletion of a tree:

>>> shutil.rmtree('python-copy')
>>> os.listdir()
['text4.txt', 'text1.txt', 'text3.txt', 'try_except.py', 'ONE', 'files.py', 'xett.txt', 'text5.txt', 'grades_oop.py', 'grades.py', 'text6.txt', 'text.txt']