Import OS # 1. switching paths ============= D = The os.getcwd () # Get the current working directory the os.chdir ( ' D: \\ ' ) # handover directory Print ( os.getcwd ()) # (how to switch it back to the past? re chdir bit on the back) os.chdir (d) Print (os.getcwd ()) # 2. order execution system ========= ==== # system and execute system commands are popen, but popen relatively easy, because it has a return value the os.system ( ' the dir ' ) # shows gbk encoding, # solutions garbled system ret = os.popen, ( ' the dir ' ) # popen value is returned, and their transcoded Print (ret.read ()) # 3. Create a folder and create a file = ========== os.mkdir ( ' the TEMP ' ) # generate a folder ,, can only generate a os.mkdir (r ' temp2 \ Inner ' ) # this being given a os.makdirs (r ' temp1 \ Inner ' , exist_ok = True) # create multi-level directory os.makdirs (r ' temp1 \ Inner \ Inner2 ' , exist_ok = True) # create multi-level directory # that if the folder already exists, on the error, if I do not want to # make an error (that is, if present, would not create nor error), then add = True exist_ok # create the file f = Open (R & lt ' temp1 \ Inner \ File' , ' W ' ) f.close () # . 4. ====== Renaming Folders ===== os.rename (R & lt ' temp1 \ Inner \ Inner2 ' , ' temp1 \ Inner \ Haiyan ' ) # 5. ===== delete folders and delete files ========= # to delete files, os.remove (r ' temp1 \ Inner \ file ' ) # and then delete the folder os.removedirs ( r ' temp1 \ Inner \ Haiyan ' ) # delete when a folder on a file if a folder is empty, along with deleted. So os.rmdir ((r ' temp1 \ Inner ' )) # removes only one folder #6. subdirectory ======== Print (os.listdir (os.getcwd ())) # print the current directory of Print (os.walk (os.getcwd ())) # <Object Generator Walk 0x00000000021C6728 aT> RET = os.walk (os.getcwd ()) # get something more, if you care about something in a subdirectory, use Walk Print (List (RET)) # 7. ==== get structural information of the file or directory Description ======== Print (the os.stat ( ' the TEMP ' )) # st_atime: time last visited # st_mtime: last modification time # st_ctime: latest update Print (os.sep) # printing is \ Print (the os.getcwd ()) file_path = 'S% S% S% ' % (The os.getcwd (), os.sep, ' filename ' ) # splicing a path (embodiment a) Print (file_path) Print (the os.path.join (The os.getcwd (), ' filename ' )) # splicing a path (second approach) # 8. ==== string indicating the current use of internet Print (the os.name) # If win, if the print is that nt, linux, print poxis # applications scene: when you enter a command, to determine a win or linux system system. Can be used # The os.name to judge the # 9. === acquisition system environment variables ===== Print (os.environ) # 10. The path associated ======= Print (the os.path. ABSPATH ( ' namedtuple.py ')) print(os.path.dirname(os.path.abspath('namedtuple.py'))) print(os.path.dirname(os.path.dirname(os.path.abspath('namedtuple.py')))) print(os.path.exists(os.path.abspath('namedtuple.py')))
Import OS # 1. switching paths ============= D = The os.getcwd () # Get the current working directory the os.chdir ( ' D: \\ ' ) # handover directory Print ( os.getcwd ()) # (how to switch it back to the past? re chdir bit on the back) os.chdir (d) Print (os.getcwd ()) # 2. order execution system ========= ==== # system and execute system commands are popen, but popen relatively easy, because it has a return value the os.system ( ' the dir ' ) # shows gbk encoding, # solutions garbled system ret = os.popen, ( ' the dir ' ) # popen value is returned, and their transcoded Print (ret.read ()) # 3. Create a folder and create a file = ========== os.mkdir ( ' the TEMP ' ) # generate a folder ,, can only generate a os.mkdir (r ' temp2 \ Inner ' ) # this being given a os.makdirs (r ' temp1 \ Inner ' , exist_ok = True) # create multi-level directory os.makdirs (r ' temp1 \ Inner \ Inner2 ' , exist_ok = True) # create multi-level directory # that if the folder already exists, on the error, if I do not want to # make an error (that is, if present, would not create nor error), then add = True exist_ok # create the file f = Open (R & lt ' temp1 \ Inner \ File' , ' W ' ) f.close () # . 4. ====== Renaming Folders ===== os.rename (R & lt ' temp1 \ Inner \ Inner2 ' , ' temp1 \ Inner \ Haiyan ' ) # 5. ===== delete folders and delete files ========= # to delete files, os.remove (r ' temp1 \ Inner \ file ' ) # and then delete the folder os.removedirs ( r ' temp1 \ Inner \ Haiyan ' ) # delete when a folder on a file if a folder is empty, along with deleted. So os.rmdir ((r ' temp1 \ Inner ' )) # removes only one folder #6. subdirectory ======== Print (os.listdir (os.getcwd ())) # print the current directory of Print (os.walk (os.getcwd ())) # <Object Generator Walk 0x00000000021C6728 aT> RET = os.walk (os.getcwd ()) # get something more, if you care about something in a subdirectory, use Walk Print (List (RET)) # 7. ==== get structural information of the file or directory Description ======== Print (the os.stat ( ' the TEMP ' )) # st_atime: time last visited # st_mtime: last modification time # st_ctime: latest update Print (os.sep) # printing is \ Print (the os.getcwd ()) file_path = 'S% S% S% ' % (The os.getcwd (), os.sep, ' filename ' ) # splicing a path (embodiment a) Print (file_path) Print (the os.path.join (The os.getcwd (), ' filename ' )) # splicing a path (second approach) # 8. ==== string indicating the current use of internet Print (the os.name) # If win, if the print is that nt, linux, print poxis # applications scene: when you enter a command, to determine a win or linux system system. Can be used # The os.name to judge the # 9. === acquisition system environment variables ===== Print (os.environ) # 10. The path associated ======= Print (the os.path. ABSPATH ( ' namedtuple.py ')) print(os.path.dirname(os.path.abspath('namedtuple.py'))) print(os.path.dirname(os.path.dirname(os.path.abspath('namedtuple.py')))) print(os.path.exists(os.path.abspath('namedtuple.py')))