Python OS module file related

Source: Internet
Author: User

Import the Module OS module using the previous import OS:

OS.SEP can replace operating system-specific path delimiters

The OS.LINESEP string gives the line terminator used by the current platform. For example, Windows uses ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '.

The Os.name string indicates the platform you are using. For example, for Windows, it's ' NT ', and for Linux/unix users, it's ' POSIX '

The OS.GETCWD () function gets the current working directory,

The Os.getenv () and os.putenv () functions are used to read and set environment variables, respectively.

Os.listdir (dirname): List directories and files under DirName

The Os.remove () function is used to delete a file.

Os.curdir: Return but previous directory ('. ')

Os.chdir (dirname): Changing the working directory to DirName

Getatime (PATH): The last access time of a file or folder, from the new era to the number of seconds it was accessed

Getmtime (PATH): Last modified time for file or folder

Getctime (PATH): The time the file or folder was created

Os.path module:

The Os.path.isfile () and Os.path.isdir () functions respectively verify whether the given path is a file or a directory, and returns a bool value

The os.path.exists () function is used to verify that the given path really exists to return bool

Os.path.getsize (name): Gets the file size, if name is directory returns 0L returns a long unit of bytes

Os.path.abspath (name): Get absolute path

Os.path.normpath (PATH): Canonical path string form, result in general/change//,

Os.path.split (name): divides name into pathname and file name, resulting in (pathname, filename. file extension) (in fact, if you use the directory completely, it will also separate the last directory as a file name, and it will not determine whether the file or directory exists)

Os.path.splitext (filename): Detach file name with extension result (filename, extension) If parameter is a path return (path, ')

Os.path.join (path,name): Connection directory with file name or directory result is Path/name

Os.path.basename (PATH): Returns the file name is actually the last "/" Partition of Path, and returns the latter. Regardless of whether the parameter is a path or the file is the same as the Os.path.split (name), the latter returns two worth of tuples

Os.path.dirname (PATH): The return file path is actually the last "/" Partition of Path, returning the former. Whether the parameter is a path or a file

The Os.system () function is used to run shell commands

The OS module wraps the common interfaces of different operating systems, allowing the user to use the same function interface to return the results of the same structure under different operating systems.

Os.name: Returns the current operating system name (' POSIX ', ' nt ', ' Os2 ', ' mac ', ' CE ' or ' riscos ')

The OS defines a set of file and path representation parameters in different operating systems, such as

Os.sep (folder delimiter, Windows is \)

OS.EXTSEP (the extension delimiter, which is in Windows.) )

OS.PATHSEP (Directory separator, Windows is; )

Os.linesep (newline delimiter, \ r \ n in Windows)

There are a number of related functions for file and path operations in the OS, such as:

Listdir (PATH): Lists all files under the directory

MakeDir (PATH): Create folder, note: Create an existing folder with exception

Makedirs (PATH): Recursive creation of folders, note: Creating an existing folder will be an exception

Remove (filename): Delete a file

RmDir (path): Delete a folder, note: Deleting a non-empty folder will be an exception

Removedirs (PATH): Recursively delete folder until one level of folder is not empty, note: folder path cannot end with ' \ '

Rename (SRC,DST): Renaming a file or folder (you can change the path, but you cannot overwrite the target file)

Renames (SRC,DST): Recursive renaming of files or filenames

Walk (PATH): Lists all files and folders under path

Process-related operations in the OS, such as:

EXECL (PATH): Runs a program to replace the current process, which can be blocked

_exit (n): Exiting the program

Startfile (filename): Runs with the program associated with the file, and immediately returns when the associated program is opened

System (CMD): Runs a program or command that returns immediately and returns a CMD exit code after cmd execution completes

Os.path: Calling different modules in different operating systems is an import module that provides a number of useful operations in this module:

Abspath (PATH): Returns the absolute path and remains if path is already an absolute path.

basename (PATH): Returns the file name in path.

Commonprefix (list): Returns the uniform prefix in the list used to get the same left-hand content of a set of strings

DirName (PATH): Returns the folder portion of path with the result not containing ' \ '

Exists (PATH): Whether a file or folder exists

Getatime (PATH): The last access time of a file or folder, from the new era to the number of seconds it was accessed

Getmtime (PATH): Last modified time for file or folder

Getctime (PATH): The time the file or folder was created

GetSize (PATH): The size of the file or folder, if the folder returns 0

Isabs (PATH): Returns whether it is an absolute path

Isfile (PATH): Returns whether it is a file path

Isdir (PATH): Returns whether it is a folder path

Islink (PATH): Returns whether it is a shortcut

Join (Path1,path2,...): Combines path and, if there is an absolute path, the previous path is deleted

Normcase (PATH): Delimiter in the transform path

Normpath (PATH): The path to the system is recognized by the conversion path

Realpath (PATH): Convert path to absolute path

Split (path): Breaks the path into (folder, file name)

Splitext (path): Breaks the path into (the remainder,. extension), if there is no extension in the file name, the extension part is an empty string

Throws a OSError exception when operating with an object that is not supported by the system.

Determine if the folder exists: >>> import os>>> os.path.exists (' d:/assist ') true>>> os.path.exists (' D:/assist /getteacherlist.py ') true>>> os.path.isfile (' d:/assist ') false>>> os.path.isfile (' d:/assist/ getteacherlist.py ') true>>> os.makedirs (' D:/assist/set ') >>> os.path.exists (' D:/assist/set ') True  Determine if the folder is empty: Use os.listdir) command >>>workpath= ' directory ' >>>if not os.list (workpath):>>> print ' Directory empty ' Traverse Folder Reference http://laocao.blog.51cto.com/480714/525140>>> def Test2 (RootDir): ... for lists in Os.listdir (RootDir ): ... path = Os.path.join (RootDir, lists) ... print path ... if Os.path.isdir (path): ... Test2 (path) ...>>> Test2 ("F:\ZR")

Python OS module file related

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.