Os.path.abspath (Path) #返回绝对路径os. Path.basename (Path) #返回文件名os. Path.commonprefix (list) #返回list (multiple paths), The longest path common to all path. Os.path.dirname (Path) #返回文件路径os. Path.exists (path) #路径存在则返回True, path corruption returned Falseos.path.lexists # Path exists returns true, path corruption also returns Trueos.path.expanduser (path) #把path中包含的 "~" and "~user" converted to user directory Os.path.expandvars (path) #根据环境变量的值替换path中包含的 "$name" and "${name}" os.path.getatime (path) #返回最后一次进入此path的时间. Os.path.getmtime (path) #返回在此path下最后一次修改的时间. Os.path.getctime (path) #返回path的大小os. Path.getsize (path) #返回文件大小, returns an error if the file does not exist Os.path.isabs (path) #判断是否为绝对路径os. Path.isfile (path) #判断路径是否为文件os. Path.isdir (path) #判断路径是否为目录os. Path.islink (path) # Determine if the path is a link os.path.ismount (path) #判断路径是否为挂载点 () os.path.join (path1[, path2[, ...]) # To synthesize a directory and file name a path os.path.normcase #转换path的大小写和斜杠os. Path.normpath (path) # Canonical path string form Os.path.realpath (path) #返回path的真实路径os. Path.relpath (path[, start]) # Calculates the relative path from start Os.path.samefile (path1, path2) #判断目录或文件是否相同os. Path.sameopenfile (FP1, FP2) #判断fp1和fp2是否指向同一文件os. Path.samestat (STAT1, STAT2) # Determines whether the stat tuple STAT1 and Stat2 point to the same file Os.path.split (path) #把路径分割成dirname和basename, returning a tuple os.path.splitdrive (path) #一般用在windows下, returns a tuple os.path.splitext (path) #分割路径 that consists of a drive name and a path, and returns a tuple of path name and file name extension Os.path.splitunc (path) #把路径分割为加载点与文件os. Path.walk (path, visit, Arg) #遍历path, entering each directory calls the Visit function, the visit function must have 3 parameters (ARG, dirname, names), DirName represents the directory name of the current directory, names represents all filenames under the current directory, and Args is the third parameter of walk os.path.supports_unicode_filenames #设置是否支持unicode路径名 *************************************
When Python is programmed, it often deals with files and directories, which is not the OS module. The OS module contains common operating system features, regardless of the specific platform. The following list of commonly used commands
1. Os.name ()--judging the platform that is now being applied, Windows returns ' NT '; Linux returns ' POSIX '
2. OS.GETCWD ()--Get the current work directory.
3. Os.listdir ()--Specify all files and directory names in all directories. Cases:
Listed in the form of a list, which does not distinguish between directories and files.
4. Os.remove ()--delete the specified file
5. Os.rmdir ()--delete the specified directory
6. Os.mkdir ()--Create a directory
Note: This can only be done by building a layer, to make it available for recursion:os.makedirs ()
7. Os.path.isfile ()--Determines whether the specified object is a file. is return true, otherwise false
8. Os.path.isdir ()--Determines whether the specified object is a directory. is true, otherwise false. Example: 9. os.path.exists ()--verifies that the specified object exists. is true, otherwise false. Example:
os.path.split ()--Returns the directory and file name of the path. Cases:
This is just the two parts of the front and back apart. is to find the last one '/'. See Example:
OS.GETCWD ()--Get the current working directory (get present work dir)
Os.system ()-Executes the shell command. Cases:
Note : When you run the shell command here, if you want to invoke a variable before python, you can do so in the following way:
Var=123os. environ [' var ']=str (Var)//Note here [] is "string" Os.system (' echo $var ')
Os.chdir ()--Change directory to the specified directory
os.path.getsize ()--Gets the size of the file, if it is a directory, returns 0
Os.path.abspath ()--Get the absolute path. Cases:
os.path.join (path, name)--Connection directory and file name. Cases:
17.os.path.basename (path)--Return file name
os.path.dirname (path)--Return file path
19. Get the actual directory where the program is located
Import Osimport sysif __name__ = = "__main__": print Os.path.realpath (sys.argv[0]) print os.path.split ( Os.path.realpath (sys.argv[0]) print os.path.split (Os.path.realpath (sys.argv[0])) [0]
Python Os.path