Discover python os module tutorial, include the articles, news, trends, analysis and practical advice about python os module tutorial on alibabacloud.com
path Os.path.dirname (path)15. Create, delete directory Os.mkdir () os.rmdir () Empty directoryGets the file name under the current directory Os.listdir (OS.GETCWD ())16. Rename file or directory Os.rename (', ')17. Change the Working directory Os.chdir ()18. Delete Multiple directories os.removedirs (' Gong/feng ')19. Read and set environment variables Os.getenv () and os.putenv ()20. Create multilevel directory Os.makedirs ()21. Get file Attributes Os.stat ()22. Modify file permissions and ti
The reference of this article is------Concise python tutorialOS is an important Python module that needs to be used before use:import OSThe usual functions are as follows:(1)Os.name, return to the platform being used.(2)os.getcwd (), returns the current working directory.(3)os.path.exists ()to verify that the given path really exists.(4)os.path.isfile ()to verify
OS module invokes the operating system and operates on files and folders1. Header filesImpoet OS2OS.GETCWD () Action: directory path for current script workPrint (OS.GETCWD ())Output:========================== restart:d:/haha/18_1.py ==========================d:\haha2.os.chdir ("directory Name") function: Change the current script working directoryOs.chdir ("d:\\python
Small series today to take a look at the basic operation of file files in Python. Now learn to buy first OH:Importing modules: Import OSFirst, we introduce a few common methods:Set Current working directory:Os.chdir ('/tmp ') #设置当前目录为tmp目录To create a folder:Os.mkdir (' Exceple ')Displays a list of files under the current working directory:Os.listdir (OS.GETCWD ()) #os. GETCWD () Get current working director
(Mastering the OS module, you need to understand the operation of common commands under Linux or Unix-like systems)os.name Indicates the platform you are using. For example, for Windows, it is ‘nt‘ , and for Linux/unix users, it is ‘posix‘ .os.getcwd()Gets the current working directory, which is the directory path of the current Python script work.Os.mkdir (' pat
information OS.SEP output operating system-specific path delimiter, win under "\ \", Linux for "/" OS.LINESEP output the current platform using the line terminator, win under "\t\n", Linux "\ N "os.pathsep output string to split file path Os.name output string indicates the current usage platform. Win-> ' NT '; Linux-> ' POSIX ' Os.system ("Bash command") runs a shell command that directly displays the Os.environ get system environment variable Os.path.abspath (PATH) Return path normalized abs
1.mkdir (path [, mode])Create only one directory, the upper directory does not throw an errorFunction: Create a directory that can be a relative or absolute path, the default mode is 0777. If the directory has multiple levels, the last level is created. If the parent directory of the last level directory does not exist, a oserror is thrown. 2.makedirs (path [, mode])-------can create a cascading directory function: Create a recursive directory tree, which can be a relative or absolute path,
The direct output system command to the screen, the method gets the command return value cannot be assigned to the variable, the output result is garbled by the encoding effect:Import Osos.system ("ipconfig")The execution of the obtained system command into memory, and then read the results in memory output to the screen, not garbled:Import= Os.popen ("ipconfig"). Read ()print(COM)Create a folder in the directory where the files are executedImport osos.makedirs ("makedir")
which path is pointing - PrintOs.path.join (R'D:'R'\3.txt')#when multiple paths are combined, the parameters before the first absolute path are ignored - PrintOs.sep#output operating system-specific path delimiter, win under "\ \", Linux for "/" in PrintOs.linesep#output The line terminator used by the current platform, win under "\t\n", Linux "\ n" - PrintOs.pathsep#output A string used to split a file path to PrintOs.name#The output string indicates the current usage platform. Win-> ' NT '; L
it is a file. Valid in Unix,windows. Os.renames (old, new) recursively renames a folder or file. Like rename ()#Shutil Moduleshutil.copyfile (SRC, DST) is copied from source Src to DST. The premise is, of course, that the destination address has writable permissions. The exception information thrown is IOException. If current DST already exists then it will be overwritten shutil.move (SRC, DST) move file or rename Shutil.copymode (SRC, DST) just copy its permissions other things will not be cop
. Valid in Unix,windows. Os.renames (old, new) recursively renames a folder or file. Like the rename () # Shutil module shutil.copyfile (SRC, DST) is copied from source Src to DST. The premise is, of course, that the destination address has writable permissions. The exception information thrown is IOException. If current DST already exists then it will be overwritten shutil.move (SRC, DST) move file or rename Shutil.copymode (SRC, DST) just copy its p
This article mainly introduces the Python OS module learning notes, this article summarizes the OS module common methods, practical methods, and gave two examples of use, the need for friends can refer to the
I. Overview of OS Mo
OS moduleProvides an interface to invoke the operating systemOS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work os.chdir ("dirname") To change the current script working directory, equivalent to the shell under Cdos.curdir return to the current directory: ('.') Os.pardir Gets the parent directory string name of the current directory: ('..') Os.make
The OS module provides new/delete/view file properties for directories or files, and also provides path actions for files and directories. For example: absolute path, parent directory ...
os.sepCan replace the operating system-specific path separator. Under Windows "\", Linux is "/"
os.linesepThe string gives the line terminator used by the current platform. For example, Windows uses ' \ r \ n ', Linux us
OS Module Common Commands python The standard library for os modules contain common operating system features. This module is especially important if you want your program to be platform-agnostic. That is, it allows a program to be written without any changes, and with
OS.SEP can replace the operating system-specific path delimiter. "\ \" under WindowsThe 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, which is the directory path of the current Python script work.Os.getenv () Gets an environment variable if none is returnedOs.putenv (key, value) sets an environment variab
information OS.SEP output operating system-specific path delimiter, win under"\\", under Linux for"/"os.linesep Output The line terminator used by the current platform, win under"\ r \ n", under Linux for"\ n"the OS.PATHSEP output string that is used to split the file path Os.name the output strings indicates the current usage platform. Win-'NT'; Linux->'POSIX'Os.system ("Bash Command"run the shell command to directly display the Os.environ get system environment variable Os.path.abspath (PATH)
, source needs absolute path
Os.utime () Modify Time Properties
>>> import os
>>> stinfo = Os.stat (' c.py ')
>>> print "Access Time ' c.py:%s \nmodified time of c.py:%s '% (stinfo.st_atime,stinfo.st_mtime)
access time of C.py:1375448908.0
modified time of c.py:1369735909.0
>>> os.utime (' c.py ', (1375448978,1369735977))
>>> print ' Access Time ' c.py:%s \nmodified time of c.py:%s '% (stinfo.st_atime,stinfo.st_mtime)
access time of c.py:1 3754489
information -OS.SEP output operating system-specific path delimiter, win under"\\", under Linux for"/" -OS.LINESEP output The line terminator used by the current platform, win under"\t\n", under Linux for"\ n" the os.pathsep output string for splitting the file path -The Os.name output string indicates the current usage platform. Win->'NT'; Linux->'POSIX' -Os.system ("Bash Command") to run the shell command to display directly - Os.environ getting system environment variables + Os.path.abspath
the shellOs.rmdir (' dirname ') Delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir dirnameOs.listdir (' dirname ') lists all files and subdirectories in the specified directory, including hidden files, and prints as a listOs.remove () Delete a fileOs.rename ("Oldname", "new") renaming files/directoriesOS.SEP Operating system-specific path delimiter, win under "\ \", L
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.