Home > other >  Python OS module in common use
Python OS module in common use

Time:09-21

The import OS

# # print (OS) getcwd ()) to obtain the current working directory
# OS. Chdir ('../') # change the current working directory (to return to the higher level), equivalent to the CD command under the sheel
# OS. Rename (' test1. TXT ', 'test. TXT) # file renaming
# OS. Remove (' test2. Py) # delete files

# # OS. The mkdir (' demo ') create a folder
# OS. Rmdir (' demo ') # delete empty folder
# OS. Removedirs (' demo ') # delete empty folder

# # print (OS) listdir ()) to list all the files and folders in the current directory; Can be introduced to the file path parameters, list all the files and folders in the specified directory
# # print (OS) name) to obtain name nt operating system: Windows operating system posix: Unix and unix-like OS
# # print (OS. Environ) get into the environment configuration
# print (OS. Environ. Get (' path ') # for the specified environment configuration


# the path variable in the OS module,
# print (OS) path) abspath (' test. TXT) # get absolute path to the file
# print (OS. Path. The exists (' test. TXT) # to determine whether a file exists, return True, there is no return False
# print (OS) path) isdir (' test. TXT) # if test. There is a TXT folder, return True, otherwise it returns False
# print (OS) path) isfile (' test1. TXT) # if test1. There is a TXT file, return True, otherwise it returns False
# print (OS) path) splitext (' test. TXT) # is used to specify the path for segmentation, access to the file suffix

CodePudding user response:

Thanks for sharing, it is what you should know
  • Related