程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

6、 Python learning notes - module -os module

編輯:Python
# os modular
"""
1、 Modules for the operating system
"""

# Introduce modules
import os
# Print the current file path
print(os.getcwd())
# Switching path
os.chdir('c:')
print(os.getcwd())
os.chdir(r'F:\python note \6、 modular \3、os modular ') # I switched to C disc , Switch back first
# Return to the current path , Relative paths
print(os.curdir)
# Return to the upper path , Relative paths
print(os.pardir)
# Create folder
# Cannot create recursively
os.mkdir(' Demo folder ')
# Delete folder
# Cannot recursively delete , Will determine whether the folder is empty , If it is blank, delete .
# Only empty folders can be deleted
os.rmdir(' Demo folder ')
# Create folder , The default generation is in the current folder
# You can recursively create multi-level folders
os.makedirs(' Demo folder ')
os.makedirs(r' Demo folder 0\1\2')
# Delete folder , Will determine whether the folder is empty , If it is blank, delete .
# You can recursively delete multi-level folders , Will determine whether the folder is empty , If it is blank, delete .
# Only empty folders can be deleted
os.removedirs(' Demo folder ')
os.removedirs(r' Demo folder 0\1\2')
# Lists the files and folders in the specified path , Returns a list of
print(os.listdir(os.getcwd()))
# Rename file 、 Folder
os.mkdir('a')
os.rename('a', 'b')
# get files 、 Folder information , return os.stat_result( Tuple format )
print(os.stat('b'))
# The file separator of the current system ,linux(/)Windows(\)
print(os.sep)
# The file newline separator of the current system ,linux(\n)Windows(\r\n)
print(os.linesep)
# The file path separator of the current system ,linux(:)Windows(;)
print(os.pathsep)
# Current system platform ,,linux(posix)Windows(nt)
print(os.name)
# perform shell command , Direct display
print(os.system('dir')) # dir yes Windows System commands
# Print environment variables
print(os.environ)
# Get absolute path through relative path
print(os.path.abspath('./b'))
# Divide the path , Split path ( Absolute path ) And file name ( Documents or folders ) Two parts , Return a tuple
print(os.path.split(os.getcwd())) # os.getcwd() For the current path ( Folder )
print(os.path.split(__file__)) # __file__ The variable is the current file
# Take out the file 、 Folder path where the folder is located , Absolute path .
print(os.path.dirname(os.getcwd())) # os.getcwd() For the current path ( Folder )
print(os.path.dirname(__file__)) # __file__ The variable is the current file
# Return the file at the end of the path ( Documents or folders ) name
print(os.path.basename(os.getcwd())) # os.getcwd() For the current path ( Folder )
print(os.path.basename(__file__)) # __file__ The variable is the current file
# Determine if it's an absolute path , return True or False
print(os.path.isabs('./b'))
# Determine if it's a file ( Non folder ), return True or False
print(os.path.isfile(__file__))
# Decide if it's a folder ( Non file ), return True or False
print(os.path.isdir(os.getcwd()))
# Combine multiple paths back
# Do not use... For path splicing ‘+’, Use join
a = '../'
b = './b'
print(os.path.join(a, b))
# Print last access time
print(os.path.getatime(__file__))
# Print last modified time
print(os.path.getmtime(__file__))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved