Discover python os module tutorial, include the articles, news, trends, analysis and practical advice about python os module tutorial on alibabacloud.com
Python: View folder permission groups and use the OS module to operate folders.
@ We recommend that you map the network drive disk when operating the server folder.
Import win32securityImport ntsecuritycon as con
FILENAME = r 'd: \ tmp \ acc_test # folder path
Sd = win32security. GetFileSecurity (FILENAME, win32security. DACL_SECURITY_INFORMATION)Dacl = sd. GetSe
Python file system features: OS module
1.os Module Method Classification
(1) Catalogue:
ChDir () Change working directory chroot () sets the root directory of the current process Listdir () lists all filenames under the specified directory
There are a lot of powerful modules in Python, where we often use OS modules, OS modules provide more than 200 methods for us to use, and these methods are related to data processing, and here's how to rename this method.
Os renaming method is Os.rename, I use the Ipython, this thing is very powerful, just press the T
located
% (created) f
Current time, represented by the UNIX standard floating-point number representing the time
% (relativecreated) d
The number of milliseconds since logger was created when the log information is output
% (Asctime) s
The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds
% (thread) d
path into directory and file name two tuples returnedOs.path.dirname (PATH)#returns the directory of path. is actually the first element of Os.path.split (path)Os.path.basename (PATH)#returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path)Os.path.exists (PATH)#Returns true if path exists, false if path does not existOs.path.isabs (PATH)#returns True if path is an absolute pathOs.path.isfile (PATH)#returns True if pa
='/home/sy/Download/chls'result=os.path.exists (filepath)Print(Result)#Isabs () detects if a path is an absolute pathPath ='/boys'result=os.path.isabs (path)Print(Result)#Samefile () detects if 2 paths are the same filepath1 ='/home/sy/Download/001'path2='.. /.. /.. /Download/001'result=os.path.samefile (path1,path2)Print(Result)#Os.environ for getting and setting built-in values for system environment variablesImportOS#Get System environment variable getenv () effectPrint(os.environ['PATH'])#S
path
Os.stat () equivalent to the Linux Stat command
Os.listdir () lists the contents of a given directory
Os.mkdir (path) Create directory
Os.mkdirs (path) Creates a directory tree, equivalent to the MKDIR-P operation SYS module
SYS.ARGV gets the arguments passed to the script, the parameter parsing is similar to the way bash, the firstA parameter represents the script itself
Sys.exit (n) exit program, Exit normally (0)
Sys.version get
This article mainly analyzes in depth the python folder permission groups and operating folders using the OS module, which has some reference value, if you are interested, refer to @. we recommend that you map the network drive disk when operating the server folder.
Import win32securityImport ntsecuritycon as con
FILENAME = r 'd: \ tmp \ acc_test # folder path
S
by the conversion pathRealpath (PATH): Convert path to absolute pathSplit (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 stringThrows 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.p
The isfile () and isdir () functions in the Python OS module return false. Solution: isfileisdir
Today, when I wrote a script for automatically backing up all directories under a specified directory in linux, I encountered a problem because I needed to back up directories, you need to determine whether the scanned file is a directory. When I use
OS ModuleOS, semantics for the operating system, contains universal operating system functionality, regardless of the specific platform. When Python is programmed, it handles files and directories such as: Show All files in the current directory/delete a file/get file Size ...OS module is not limited by the platform, t
The OS module is a python built-in module,mainly on a large number of files and a large number of paths to operate OS.SEP: Supersedes the operating system-specific path delimiter Os.name: Indicates the work platform you are using. For example, for Windows, it's ' NT ', and for Linux/unix users, it's ' POSIX '. OS.GETCW
value passed in (command line: Python filename parameter value 1 parameter value 2), (Command line argument list, first element is the program itself path) View Practice 7clear_log.py 7 #sys.exit () #退出程序, exit Normal exit (0)8 #quit (' program quit ') #跟上面一样, do not execute the following code9 #sys.version #获取Python解释程序的版本信息Ten #Sys.maxint #最大的Int值 One #Sys.path #返回模块的搜索路径, using the value of the PYTHONPA
Tags: style blog http color using OS strong IOResources:Mac OS X Python installation mysqldb module http://www.codeif.com/post/1073/ MAC OSX uses Python to install modules for issues http://www.myexception.cn/operating-system/1616547.htmlMySQL and Django are installed in
Python OS module Common methods:OS.GETCWD () Gets the current directory, similar to the PWD in the shell. Os.chdir (' dirname ') switches the directory to the same as the CD in the shell. Os.makedirs (' dirname1/dirname2 ') cascading creates directories and mkdir-p in the shell like Os.removedirs (' dirname1 ') if the directory is empty, then deleted, and recursi
The OS module contains common operating system features.Note: The function parameter path is a file or directory, filename is the path to the file, DirName is the path to the directory, the path can be a relative path, or an absolute pathCommon or important functions are bold fontsOS module:Os.name returns the current operating system name (' POSIX ', ' nt ', ' Os2 ', ' mac ', ' CE ', ' riscos '), Windows '
correspond to standard I/O streams. If you need more control over the output, and print does not meet your requirements, they are what you need. You can also replace them, so you can redirect output and input to other devices, or handle them in a non-standard wayThird, PaltformPlatform.system () Get OS type, Windows, Linux, etc.Platform.platform () Get the operating system, Darwin-9.8.0-i386-32bitPlatform.version () Get System version information 6.2
One, OS moduleImport OS, sysOs.system (' ipconfig ') #执行操作系统命令, get no results returnedOs.popen () #也可以执行操作系统命令, can return command execution result, but need read outPrint (Os.popen (' dir '). Read ())Second, sys modulePrint (SYS.ARGV) #命令行参数List, the first element is the path of the program itself, the parameters that are entered when the file is executedExample: Writing the input to a fileInpu = sys.a
The SYS module can obtain command-line parameters, implement the function of passing parameters from outside the program to the program, and obtain the program path and the information of the current platform.
1.sys.plateform Current operating system platform
2. Command line arguments
SYS.ARGV: Implements passing parameters from outside the program to the program.
#!usr/bin/python
#coding: UTF8
import sys
Summary of common functions of Python OS moduleThe OS module provides a unified operating system interface function, which is usually specified by the platform, and the OS module can automatically switch between specific functions
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.