OS module commonly used functions:
Get the current path os.getcwd ()
Get file and directory names in the specified path os.listdir ( '../')
Specify the path to get the absolute path os.path.abspath ()
Separation (folder path + file name) The os.path.split () of the specified path
Path and file merge os.path.join ()
Extract the specified portion of the path of the folder and file names os.path.dirname () os.path.basename ()
to determine the path exists os.path.exists ()
recursively create directories os.makedirs ()
recursively delete the directory os.removedirs ()
Determining whether the directory path os.isdir ()
Determines whether the file path os.isfile ()
. 1 Import numpy AS NP 2 Import OS . 3 . 4 . 5 # Path operation . 6 DEF Test01 (): . 7 # Get the current path . 8 Print ( " Get current path The os.getcwd (): " , The os.getcwd ()) . 9 # give designated file and directory names in the path 10 Print ( " get file and directory names in the specified path os.listdir ( '../'): " , os.listdir ( ' ../ ' )) . 11 12 is # acquired (specified ) the absolute path paths 13 Print ( "Obtained absolute path specified path os.path.abspath with: " , os.path.abspath with ( ' ../ ' )) 14 15 # separation (folder path + file name) of the specified path 16 Print ( " # specified separating path (folder path + file name) the os.path.split " ) . 17 the os.path.split ( ' / Home / FH / image /11-08/json_to_dataset.py ' ) 18 is . 19 # combined os.path. the Join 20 is Print ( " the os.path.join, path and file merge " ) 21 is the os.path.join ( ' / Home / FH / image /11-08/json_to_dataset.py ' , '/ home / nufront / image /11-08/getimg_label.py ' ) 22 is the os.path.join ( ' / Home / FH / image / 11-08 ' , ' getimg_label.py ' ) 23 is the os.path.join ( ' ../dataset_learn ' , ' data_learn.py ' ) 24 the os.path.join ( ' .. ' , ' dataset_learn ' ) 25 26 is # extract the file name and the folder portion 27 os.path.dirname ( ' / Home / FH / picture /11-08/json_to_dataset.py ') 28 os.path.basename ( ' / Home / FH / picture /11-08/json_to_dataset.py ' ) 29 30 # to view the directory or file exists 31 os.path.exists ( ' / Home / FH / Pictures / 11-08 /json_to_dataset.py ' ) 32 os.path.exists ( ' / Home / FH / image / 11-08 ' ) 33 is os.path.exists ( ' / Home / FH / image / 11-08 / 3423 " ) 34 is 35 # create a directory 36 os.mkdir ( ' testdir ' ) 37 # delete a directory 38 os.rmdir ( 'the testdir ' ) 39 40 # Create recursive directory 41 is os.makdirs ( ' ./test/disrs/testdir ' ) 42 is # recursively delete directory 43 is os.removedirs ( ' ./test ' ) 44 is 45 Print ( ' change directory os. the chdir () ' ) 46 is 47 Print ( ' determines whether the directory path os.isdir () ' ) 48 49 Print ( ' determines whether a file path os.isfile () ' ) 50 51 is 52 is if __name__ == 'main': 53 test01()