Python – Module os
To import the module type:
import os
Main functions of os
Functions | What they do |
---|---|
os.path.join() | To use system folders separator / (UNIX/MACOS) or \\ (WIN) and create a path from different strings. |
os.sep | To know which separator is used on your system |
os.path.getsize() | Will return the size of a file (only works on files) |
os.listdir() | This function returns a list of all files and folders of a directory |
os.getcwd() | To get the current directory |
os.path.chdir() | You can change of level with ‘..’ or ‘\\’ |
os.path.makedirs() | To create a directory |
os.unlink(‘filename’) | To delete permanently a single file (old method) |
os.rmdir(‘directory’) | To delete an empty folder // for more options see Move, copy and delete files module |
os.path.dirname() | It will return the directory name from a complete path |
os.path.basename() | It will return the file name only from a complete path If no filename is mentioned it will get the last folder name |
os.path.basename(‘full_path’)
It will return the file name only (without directories if included in the string). If no filename is mentioned it will get the last folder name
Code | Result |
---|---|
os.path.basename (‘C:\\Python\\Python38-32‘) | ‘Python38-32‘ |
os.path.basename (‘C:\\Python\\Python38-32\\spam.txt’) | ‘spam.txt’ |
os.path.abspath()
It will return a complete file path from a relative file path.
For example if your current path is ‘C:\\Python\\Python38-32’ and
Code | Result |
---|---|
os.path.abspath(‘spam.txt’) | ‘C:\\Python\\Python38-32\\spam.txt’ |
os.path.abspath(‘..\spam.txt’) | ‘C:\\Python\\spam.txt’ |
os.path.relpath(‘Origin_File_Path’, ‘Actual_Path’)
It will return the relative command path to reach the file indicated in the first argument from the path mentioned in second place
Code | Result |
---|---|
os.path.relpath(‘C:\\spam.txt’, ‘C:\\Python\\Python38-32‘) | ‘..\\..\\spam.txt’ |
os.path.relpath(‘C:\\Python\\Python38-32\\spam.txt’, ‘C:\\Users’) | ‘..\\Python\\Python38-32\\spam.txt’ |
os.walk(‘Path’)
This function will return a generator object. To read the values, you will need a for loop to read values.
The different values returned are: foldername, subfoldername, filename.
import os for folderName, subfolders, filenames in os.walk('C:\\__PRODS__\\testdir'): print('The current folder is ' + folderName) for subfolder in subfolders: print('SUBFOLDER OF ' + folderName + ': ' + subfolder) for filename in filenames: print('FILE INSIDE THE FOLDER : ' + filename) print('')
The code above will give this result:
The current folder is C:\__PRODS__\testdir
SUBFOLDER OF C:\__PRODS__\testdir: dir001
SUBFOLDER OF C:\__PRODS__\testdir: dir002
SUBFOLDER OF C:\__PRODS__\testdir: dir003
The current folder is C:\__PRODS__\testdir\dir001
FILE INSIDE THE FOLDER : test001.txt
FILE INSIDE THE FOLDER : test002.txt
FILE INSIDE THE FOLDER : test003.txt
The current folder is C:\__PRODS__\testdir\dir002
FILE INSIDE THE FOLDER : test002.txt
The current folder is C:\__PRODS__\testdir\dir003
FILE INSIDE THE FOLDER : test003.txt
os.path: Useful tests (True / False)
Functions | What they do |
---|---|
.exists() | Check if path exists |
.isabs() | Check if path is absolute (True) or relative (False) |
.isfile() | Check if the path entered is a file |
.isdir() | Check if the path entered is a folder |