The basic operation modules python os

# Os module directory built-in library-related 
Import os
# current directory .. go back one level.
# 1. os.path.abspath () - Gets the absolute path of the current file (not including the os module .py) pwd
# path = os.path.abspath ( ".")
# Print (path) #D: \ Starter Test \ Python basis \ exercise

# 2. os.path.realpath (__ file__) - get the full path of the current file
# print (os. path.realpath (__ file__)) #D: \ Test \ Python Basics \ practice \ os module .py

# 3. os.path.dirname () - gets the current absolute file path pwd
# Print (os.path.dirname (os.path.realpath (__ file__))) #D: \ Test \ Basics Python \ exercise

# 4. os.path.dirname (os.path.dirname (os.path.realpath ( __ file__))) - Gets on a path to an absolute path
# print (os.path.dirname (os.path.dirname (os.path.realpath (__ file__)))) #D: \ Test \ Python Basics

# 5. os.path.join (a, b) splicing the two directory paths
# print (os.path.join (os.path.dirname (os.path.realpath (__ file __)), "os module .py"))
Os.path.dirname pwdpath = # (the os.path.realpath (__ file__))
# = pjpath the os.path.join (pwdpath, "OS module .py")
# Print ( "full path splicing:", pjpath)
# print ( "full path to the stitched:", the os.path.realpath (__ file__))
# full path to the mosaic: D: \ Test \ Basics Python \ practice \ os module .py
the full path to the # splicing: D: \ Test \ Basics Python \ practice \ os module .py

# 6. os.path.splitext () to separate out the file extension and file
# filepath = the os.path.realpath (__ file__)
# Print ( "get full path to the file: ", filepath)
# = fileParh os.path.splitext (filepath)
# print (" Get the delimited file: ", fileParh) # returns a tuple format
# print (" Get rear partition file suffix: ", fileParh [-1])
the full path to # obtain files: D: \ Test \ Basics Python \ practice \ os module .py
# obtain files after separation: ( 'D: \\ Test \\ Python exercise Basics \\ \\ os module ',' .py ')
# get separated after the file name suffix: .py

# 7. os.path.normpath () - irregular path becomes a path specification
Str1 = # "E: \ Program Files (the x86) \ 360 \ 360Safe \ Config \ /// advtools \ WenJianFenSuiJi.xml"
# Print ( "canonical path:", os.path.normpath (str1))

#. 8. other methods
"" "
os.chdir (path =" path ") switching paths
os.getcwd () to get the current directory - the absolute path
os.mkdir () to create a directory
os.listdir () lists all the files in the current directory and directory
"" "

# 9. Analyzing files and directories
" ""
os.path.isdir ( "directory path") -> return exists True, the anti False
the os.path.isfile ( "file path") - > there is return True, the anti-False
"" "

# os module combat
# print out the C: \ All .dll files in the Program files \ Internet Explorer directory
os.chdir (" C: \ Program files \ Internet Explorer ")
# Print ( "current directory:", os.getcwd ())
# Print ( "all files and directories under the current directory:", os.listdir())
List = os.listdir()
for index in List:
if ".dll" in index:
print ( "ending in .dll files:", index)

Guess you like

Origin www.cnblogs.com/Teachertao/p/11707865.html