Detailed explanation of os module of python

os module details

When looking at the great gods' code, you can often see the figure of the os module, and then want to make a summary for later viewing
The following figure refers to CSDN blogger's "beauty of data analysis and statistics". Thank you very much for the picture

Let's introduce the usage of each one in detail:
(1) os.getcwd() gets the current working path;

>>> import os
>>> os.getcwd()
'C:\\Users\\cc'

(2) os.listdir(path) displays a list of all files and directories in the current folder;

>>> path=os.getcwd()
>>> os.listdir(path)
['.anaconda', '.android', '.AndroidStudio3.5', '.astropy', '.bash_history', '.cache', '.conda', '.condarc', '.config', '.designer', '.emulator_console_auth_token', '.gitconfig', '.gradle', '.ipython', '.jupyter', '.keras', '.labelmerc', '.liarcom-gui.cfg', '.matplotlib', '.spyder-py3', '.ssh', '1.3.6.1.4.1.14519.5.2.1.6279.6001.861997885565255340442123234170.mhd', '1.3.6.1.4.1.14519.5.2.1.6279.6001.861997885565255340442123234170.zraw', '3D Objects', 'Anaconda3', 'ansel', 'AppData', 'Application Data', 'Contacts', 'Cookies', 'Datasets', 'Desktop', 'Documents', 'Downloads', 'Favorites', 'Links', 'Local Settings', 'MicrosoftEdgeBackups', 'Music', 'My Documents', 'NetHood', 'NTUSER.DAT', 'ntuser.dat.LOG1', 'ntuser.dat.LOG2', 'NTUSER.DAT{53b39e87-18c4-11ea-a811-000d3aa4692b}.TxR.0.regtrans-ms', 'NTUSER.DAT{53b39e87-18c4-11ea-a811-000d3aa4692b}.TxR.1.regtrans-ms', 'NTUSER.DAT{53b39e87-18c4-11ea-a811-000d3aa4692b}.TxR.2.regtrans-ms', 'NTUSER.DAT{53b39e87-18c4-11ea-a811-000d3aa4692b}.TxR.blf', 'NTUSER.DAT{53b39e88-18c4-11ea-a811-000d3aa4692b}.TM.blf', 'NTUSER.DAT{53b39e88-18c4-11ea-a811-000d3aa4692b}.TMContainer00000000000000000001.regtrans-ms', 'NTUSER.DAT{53b39e88-18c4-11ea-a811-000d3aa4692b}.TMContainer00000000000000000002.regtrans-ms', 'ntuser.ini', 'OneDrive', 'Pictures', 'PrintHood', 'PycharmProjects', 'Recent', 'Saved Games', 'Searches', 'SendTo', 'Templates', 'UIDowner', 'Videos', '「start」menu']

(3)os.walk(path)
Meaning: Meaning: pass in any path, deeply traverse all subfolders under the specified path, and return a tuple composed of path, folder list and file list
(4)os.path.exists(path)
Function: pass in a path to judge whether the directory under the specified path exists. If it exists, return True; otherwise, return False;

>>> import os
>>> os.getcwd()
'C:\\Users\\cc'
>>> path=os.getcwd()
>>> os.path.exists(path)
True

(5)os.mkdir(path)
Meaning: pass in a path to create a single folder;

 >>> os.getcwd()
'C:\\Users\\cc'
>>> path2 = os.getcwd()+"\\cuicui"
>>> os.mkdir(path2)
>>> path2
'C:\\Users\\cc\\cuicui'

(6) os.makedirs(): pass in a path path and generate a recursive folder
Note: if the folder already exists, an error will be reported. Therefore, before creating the folder, you need to use the os.path.exists(path) function to determine whether the folder exists;

 if os.path.exists("./weights") is False:
        os.makedirs("./weights")  # Function: new folder
    
 >>> os.getcwd()
'C:\\Users\\cc'
>>> path1=os.getcwd()+"\\cuicui"
>>> os.mkdir(path1)
>>> path1
'C:\\Users\\cc\\cuicui'
>>> os.makedirs("cuicui\\1")
>>> path2=os.makedirs("cuicui\\1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\cc\Anaconda3\lib\os.py", line 220, in makedirs
    mkdir(name, mode)
FileExistsError: [WinError 183] Cannot create a file when it already exists.: 'cuicui\\1'

(7)os.rmdir(path)
Meaning: pass in a path and delete the folder under the specified path;
Note: this method can only delete empty folders. Deleting non empty folders will report an error;

>>> os.rmdir('cuicui//1//2')

(8)os.path.join(path1,path2)
Meaning: pass in two path paths and splice them to form a new complete path

>>> path2
'C:\\Users\\cc\\a\\b\\c'
>>> f="1.txt"
>>> os.path.join(path2,f)
'C:\\Users\\cc\\a\\b\\c\\1.txt'
>>>

(9)os.path.split(path)
Meaning: pass in a complete path and split it into absolute path and file name

>>> path3=os.path.join(path2,f)
>>> os.path.split(path3)
('C:\\Users\\cc\\a\\b\\c', '1.txt')
>>>

(10)os.path.dirname(path)
Meaning: pass in a complete file path and only get its absolute path;

>>> path3=os.path.join(path2,f)
>>> os.path.split(path3)
('C:\\Users\\cc\\a\\b\\c', '1.txt')
>>> path4=os.path.dirname(path3)
>>> path4
'C:\\Users\\cc\\a\\b\\c'
>>>

(11)os.path.basename(path)
Meaning: pass in a complete file path and only get its file name;

>>> path3
'C:\\Users\\cc\\a\\b\\c\\1.txt'
>>> path5=os.path.basename(path3)
>>> path5
'1.txt'

(12)os.path.isdir(path)
Meaning: pass in a complete file path to judge whether it is a folder;

path = os.getcwd()
file_list = os.listdir()
for file in file_list:
    if os.path.isdir(file):
        print(file)

(13)os.path.isfile(path)
Meaning: pass in a complete file path to judge whether it is a file;

path = os.getcwd()
file_list = os.listdir()
for file in file_list:
    if os.path.isfile(file):
        print(file)

(14)os.path.sep
Meaning: returns the path separator of the current operating system;

>>> os.path.sep
'\\'

(15)os.path.getsize(path)
Meaning: pass in a complete file path and return the size of the file;

>>> os.path.getsize("cuicui")
0

References: (1) https://blog.csdn.net/weixin_41261833/article/details/108047966?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163784090316780269868868%2522%252C%2522scm%2522%253A%252220140713.130102334...%2522%257D&request_id=163784090316780269868868&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_positive~default-1-108047966.first_rank_v2_pc_rank_v29&utm_term=+os%E6%A8%A1%E5%9D%97&spm=1018.2226.3001.4187
(2)https://blog.csdn.net/qq_38684504/article/details/86609283?ops_request_misc=&request_id=&biz_id=102&utm_term=%20os%E6%96%87%E4%BB%B6%E7%9A%84%E5%90%84%E7%A7%8D%E7%94%A8%E6%B3%95&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-0-86609283.first_rank_v2_pc_rank_v29&spm=1018.2226.3001.4187

Keywords: neural networks Deep Learning NLP

Added by ex247 on Fri, 26 Nov 2021 18:38:29 +0200