Discover python os module tutorial, include the articles, news, trends, analysis and practical advice about python os module tutorial on alibabacloud.com
The OS module is an operating module in the Python standard library, primarily for processing files and directories in the Linux operating system1. To use the OS, you must first import the OSImport OS2.OS.GETCWD () Gets the current path.print os.getcwd ()3.os.listdir () List
Python OS module important points of knowledgeThis is very important, mainly about the file path, I have stepped on a lot of pits, today summed up, convenient to avoid the path associated with the various pits!1, first we want to get all the folders under a folder and files ( not including files in subfolders)Lists = Os.listdir (Path)2, want to get all the folder
1. Chardet ModulePython is dealing with string problems and often encounters string encoding problems. Chardet is a very good code recognition module.The formats that can be identified are:
ASCII, UTF-8, UTF-16 (2 variants), UTF-32 (4 variants)
Big5, GB2312, EUC-TW, hz-gb-2312, ISO-2022-CN (traditional and Simplified Chinese)
EUC-JP, Shift_JIS, CP932, ISO-2022-JP (Japanese)
EUC-KR, Iso-2022-kr (Korean)
Koi8-r, Maccyrillic, IBM855, IBM866, Iso-8859-5, windows-1251 (Cyrill
Import os# 1. Switch path =============d = OS.GETCWD () #获取当前的工作路径os. ChDir (' d:\\ ') #目录的切换print (OS.GETCWD ()) # (Toggle the past how to come back?) I'll be back in a chdir. Os.chdir (d) Print (OS.GETCWD ()) # 2. Execute the System commands =============# system and Popen are all execution systems commands, but popen is more useful, Because it has a return value of Os.system (' dir ') #显示的是gbk的编码, # solv
can improve compatibility3.1 Json.dumps () Import jsondic={'Alex':'+ '# Note the JSON-type string , all in double quotes, at which point the single quote a=json.dumps (DIC)print(a) {"Alex " " - "} # automatically outputs into double quotes, and is compatible with each platformThere is a shortcut, Json.dump (' Sjah ', f)The former is the data that needs to be written, and the latter is the file to be written, which is relatively fast3.2 Json.loads ()Take the JSON type string out, note that i
The Python OS module provides a unified operating system interface function One, for the operation of the system 1, Os.name is currently using the platform where ' NT ' is windows, ' POSIX ' is a specific path delimiter for Linux or unix2, OS.SEP output operating system. Win under "\", Linux for "/" 3, OS.PATHSEP output divider output for splitting the file path
Common built-in methods__doc__ Printing Notes__package__ Printing the same package__cached__ Print byte code__name__ Current main module is __name__ = = __main____file__ Print File pathCommon OS ModulesOS.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 wo
The OS modules in the Python standard library 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 or problems, and can be run under Linux and Windows.
Some of the more useful parts of the
First, the OS module is the module used to interact with the operating system, which can operate on something on the operating system.And SYS is used to perform some operations on the interpreter.First, OSOS.GETCWD () Gets the current working directory, that is, the directory path of the current Python script work os.c
Under the specified path, searching for the specified content contained in the Excel file, you first need to traverse the specified path to get the absolute/relative path of all Excel files under that path, and then read the contents of Excel, judging the value of each cell in the file and the content to be searched ( Regular comparison, equivalent comparison ). Therefore, implementing this feature requires two parts of the content, path traversal , and Excel file content reading .Use the
Log for the program to run and technical personnel is very necessary and very important, troubleshooting is generally from the analysis program to run the log, and then the complexity of the large program must have a log input, otherwise, even if the program is not qualified. Python provides a handy logging module for technicians to define and output logs.Let's take a look at the log level and the simple ou
Some of the actions associated with the interpreter in the SYS module, some of the system-related operations in the OS moduleSYS modulesys.argv command line argument list, the first element is the program itself path Sys.exit (n) exits the program, exit normally (0) sys.version Gets the version information of the Python interpreter Sys.max
nameOs.path.dirname (PATH): Return file path1. Os.path methodBy passing in the directory that needs to be traversed, listing all the files in the directory and counting the number of files, the OS-provided path module can operate very flexibly on the directory.Import Os,sysdef listdir (dir,file):File.write (dir + ' \ n ')Fielnum = 0List = Os.listdir (dir) #列出目录下
The OS module provides a number of features that allow your program to interact directly with the operating system to get the current working directory, that is, the directory path of the current Python script: OS.GETCWD () is not a script path returns all files and directory names under the specified directory: Os.listdir () function to delete a file: os.remove
This module contains common operating system functions. 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 or problems, and can be run under Linux and Windows. An example is os.sep the use of a path delimiter that can replace an operating system specific.Some os of t
") Rename File/Catalog Os.stat ('Path/filename') Get File/directory 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 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 O
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.