Python common directory operations

Python3 common directory operations

    #导入os模块
	import os

	# 创建目录
	os.mkdir('E:\\PythonProject\\testdir')

	# 删除目录
	os.rmdir('E:\\PythonProject\\testdir')

	# 创建多级目录
	os.makedirs('E:\\PythonProject\\testdir\\testdir2')
	
	# 删除多级目录
	os.removedirs('E:\\PythonProject\\testdir\\testdir2')
	
	# 获取目录下文件夹及文件
	path = os.listdir('E:\\PythonProject')
	for ep in path:
    	print(ep)
    	
	# 获取当前目录位置`在这里插入代码片`
	print(os.getcwd())
	
	# 切换目录
	os.chdir('E:\\360Downloads')
	print(os.getcwd())
	
	# 遍历所有子目录以及文件
	for parent,dirnames,filenames in os.walk('E:'+os.sep+'PythonProject'):
    	for filename in filenames:
        	file_path=os.path.join(parent,filename)
        	print(file_path)

os.path common method library

name meaning
os.path.abspath(path) Returns the absolute path
os.path.basename(path) Returns the file name
os.path.commonprefix(list) Return list (multiple paths), all of the common path of the longest path
os.path.dirname(path) Returns the file path
os.path.exists(path) Path exists returns True, and False path damaged
os.path.lexists Returns True path exists, the path returns True damage
os.path.expanduser(path) The path in which " " and " User" is converted into the user directory
os.path.expandvars(path) Alternatively path based on the value contained in the environment variable " n a m e " with " name"和" {name}"
os.path.getatime(path) Returns the last access time (floating-point number of seconds)
os.path.getmtime(path) Returns last file modification time
os.path.getctime(path) Returns the file path creation time
os.path.getsize(path) Returns the file size, file does not exist if it returns an error
os.path.isabs(path) Determine whether the absolute path
os.path.isfile(path) Determine whether the file path
os.path.isdir(path) Determine whether the directory path
os.path.islink(path) Determine whether the link path
os.path.ismount(path) Determining whether the path is a mount point
os.path.join(path1[, path2[, …]]) The synthesis of a directory and file name path
os.path.normcase(path) Conversion path capitalization and slash
os.path.normpath(path) Specification path string
os.path.realpath(path) Return true path path
os.path.relpath(path[, start]) Counted from the start relative path
Oskpthksmefaile (Pthl, Pth2) Determine whether the same file or directory
os.path.sameopenfile(fp1, fp2) Fp1 and fp2 point to determine whether the same file
os.path.samestat (stat1, stat2) Judgment stat tuple stat1 and stat2 point to the same file
os.path.split(path) The path is divided into dirname and basename, returns a tuple
os.path.splitdrive(path) Generally used in the windows, the drive and path returns a tuple
os.path.splitext(path) Split path, return path and file name extensions tuple
os.path.splitunc(path The route into a mount point and file
os.path.walk(path, visit, arg) Traversal path, into each directory function calls the visit, visit function must have three parameters (arg, dirname, names), dirname represents the directory name of the current directory, names on behalf of all the files in the current directory name, args, compared walk of The third parameter

Guess you like

Origin blog.csdn.net/qq_21153997/article/details/91796975