Python Learning Notes--common object methods for OS modules

Source: Internet
Author: User

1 #here is a list of the functions used in the OS module for files/directories2 3 #note here that before using these methods, remember to import the OS module4 ImportOs#Import OS module5 """6 OS Object methods:7 os.getcwd () #返回当前工作目录8 os.chdir (path) #改变工作目录9 Os.listdir (path= ".") #列举指定目录中的文件名 ("." Represents the current directory, "..." Indicates the previous level of the directory)Ten os.mkdir (Path) #创建建单层目录, throws an exception if the directory already exists One os.makedirs (path) #递归创建多层目录, if a thrown exception already exists in the directory, A os.remove (path) #删除文件 - os.rmdir (Path) #删除单层目录, throws an exception if the directory is not empty - os.removedirs (Path) #递归删除目录, from sub-directory to parent directory by layer, try to delete, encountered directory non-empty throws an exception the os.rename (old,new) #将文件old重命名为new - os.system (command) #运行系统shell命令 - os.walk (top) #遍历top路径以下所有子目录, returns a ternary group: (Path, [contains directory],[include file]) - Os.curdir #属性 that represents the current directory + Os.pardir #属性, indicating the previous level of directory - os.sep #属性, output operating system-specific path delimiter (win under ' \ \ ', Linux as '/') + Os.linesep #属性, the line terminator used by the current platform (win under ' \ r \ n ', Linux ' \ n ') A Os.name #属性 refers to the operating system that is currently in use at      - Os.path Object methods: - os.path.basename (path) #去掉目录路径, return file name separately - os.path.dirname (path) #去掉文件名, return the directory path separately - Os.path.join (path1[,],path2[,...]) #将path1, path2 parts form a path name - os.path.split (path) #分割文件名和路径, returns a (f_path,f_name) tuple that separates the last directory as a file name if the directory is fully used in os.path.splitext (path) #分离文件名和后缀名, returns the (f_name,f_extension) tuple, which separates the last directory as the file name if the directory is fully used - os.path.getsize (file) #返回指定文件的尺寸, Unit is byte to os.path.getatime (file) #返回指定文件最近的访问时间 (floating-point number of seconds, gmtime () or localtime () function of the time module can be converted) + os.path.getctime (file) #返回指定文件的创建时间 - os.path.getmtime (file) #返回指定文件最新的修改时间 the os.path.exists (path) #判断指定路径是否存在 (directory or file) * os.path.isabs (path) #判断是否为绝对路径 $ os.path.isdir (path) #判断指定路径是否存在且是一个目录Panax Notoginseng os.path.isfile (path) #判断指定路径是否存在且是一个文件 - os.path.islink (path) #判断指定路径是否存在且是一个符号链接 the os.path.ismount (path) #判断指定路径是否存在且是一个挂载点 + os.path.samefile (path1,path2) #判断path1, does path2 point to the same file A          the """

Python Learning Notes--common object methods for OS modules

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.