1.os Module 1.1 function
File directory management, process management
Http://www.cnblogs.com/now-fighting/p/3531464.html
1.2 Common
os.name--determine which platform is now in use. Windows--nt; Linux--posix
OS.GETCWD ()--Get the current work directory.
Os.listdir (Path)--Specify all files and directory names in all directories and return to list.
Os.remove (Path)--delete the specified file
Os.mkdir (path [, mode=0777])--Create Directory
Os.makedirs (path [, mode=0777])--Create a directory recursively
Os.path.isfile (Path)--Determines whether the specified object is a file. is return true, otherwise false
Os.path.isdir (Path)--Determines whether the specified object is a directory. is true, otherwise false.
Os.path.exists (Path)--verifies that the specified object exists. is true, otherwise false.
Os.path.split (PATH)--Returns the directory and file name of the path, in tuples. Cases:
1 ImportOS2 3 ImportSYS4 5 6 7 if __name__=="__main__":8 9 PrintOs.path.realpath (sys.argv[0])Ten One PrintOs.path.split (Os.path.realpath (sys.argv[0])) A - PrintOs.path.split (Os.path.realpath (sys.argv[0)) [0] - the - -***Execution Results - +/home/jihite/ftp/del. PY - +('/home/jihite/ftp','del.py') A at/home/jihite/FTP - - - - os.system (command)-executes the shell command. Example: - in>>> Os.system ("echo ' Hello world! '") - to 'Hello world!' + - 0 the * Note: When you run the shell command here, if you want to invoke a variable before python, you can do so in the following way: $ Panax NotoginsengVar=123 - theos.environ['var']=str (VAR)//notice here [] is "string" + AOs.system ('Echo $var')Os.path.spilt Use Example
Os.chdir (Path)--Change directory to the specified directory
Os.path.getsize (filename)--Gets the size of the file, if it is a directory, returns 0
Os.path.join (path, name)--The connection directory and file (directory) name.
Os.walk (Path)--recursively traverse a folder (including subfolders)
Walk (Top, topdown=true, Onerror=none, Followlinks=false)
1>>>OS.GETCWD ()2 3 'E:\\yc_study\\python\\study_imooc\\file_handle'4 5>>> forDir_path,dir_name,file_namesinchOs.walk (OS.GETCWD ()):6 7...Print '*****dir_path\n'8 9...PrintDir_pathTen One...Print '*****dir_name\n' A -...PrintDir_name - the...Print '*****file_name\n' - -...PrintFile_names - + ... - +*****Dir_path A at - - E:\yc_study\python\study_imooc\file_handle - -*****Dir_name - in - to['1'] + -*****file_name the * $ Panax Notoginseng['1.txt','argv.py','Create_utf_8.txt','helloworld.py','Unicode_test.txt'] - the*****Dir_path + A the +E:\yc_study\python\study_imooc\file_handle\1 - $*****Dir_name $ - - the['2'] - Wuyi*****file_name the - Wu - [] About $*****Dir_path - - - AE:\yc_study\python\study_imooc\file_handle\1\2 + the*****Dir_name - $ the the['3'] the the*****file_name - in the the [] About the*****Dir_path the the + -E:\yc_study\python\study_imooc\file_handle\1\2\3 the Bayi*****Dir_name the the - - [] the the*****file_name the the - the [] the the
Os.walk Use Example
2.shutil Module 2.1 function
Move, copy, package, compress, unzip
http://blog.csdn.net/xmnathan/article/details/36217631
2.2 Common commands
Shutil.rmtree ("dir")--Deletes the directory. Empty directories, contents of the directory can be deleted
CopyFile (SRC, DST) # Copy files
Copies the contents of the file (without metadata) from SRC to DST.
DST must be the full destination file name.
If SRC and DST are the same file, an error shutil will be thrown. Error.
DST must be writable, otherwise an exception IOError will be thrown.
If DST already exists, it will be replaced.
Special files, such as characters or block devices and pipelines, cannot use this feature because CopyFile opens and reads the file.
SRC and DST are path names in the form of strings.
CopyFile () calls the bottom function layer copyfileobj ().
Shutil.copyfile (' shutil_copyfile.py ', ' shutil_copyfile.py.copy ')
Copyfileobj (FSRC, FDST, length=16384) # Copy the contents of the file to another file
Need to introduce Stringio module to use
Copies the contents of the file (without metadata) from the class file object src to the class file to DST.
Optional parameter length Specifies the size of the buffer, and a negative number indicates a one-time read-in.
By default, the data is cut into small copies to avoid taking up too much memory.
Note: The copy is starting from the current file of FSRC.
Copy (SRC, DST)
Copy the file src to the file or directory DST.
The permission bits are also copied.
If DST is a directory, create (or overwrite) the same file name using SRC.
SRC and DST are path names in the form of strings.
Os.mkdir (' example ')
print ' Before: ', Os.listdir (' example ')
Shutil.copy (' shutil_copy.py ', ' example ')
Copy2 (SRC, DST)
Similar to shutil.copy, meta data is also duplicated.
Actually call Shutil.copy first, and then use Copystat.
Shutil.copy2 (' shutil_copy2.py ', ' example ')
Copytree (SRC, DST, Symlinks=false, Ignore=none) # recursive replication
Recursively copy a directory tree using Copy2 ().
3.glob Module
You can find files for your own purposes , similar to file searches under Windows, and also support wildcards, *,?, [] These three wildcards, * represent 0 or more characters,? represents a character, [] matches a character within a specified range, such as [0-9] matches a number.
3.1glob.glob (PATH)
Return a list of paths matching a pathname pattern.
3.2glob.igblob ()
Return an iterator which yields the paths matching a pathname pattern.
1>>>ImportOS2 3>>> Os.chdir (r'E:\yc_study\python\study_imooc\file_handle')4 5>>>OS.GETCWD ()6 7 'E:\\yc_study\\python\\study_imooc\\file_handle'8 9>>>ImportGlobTen One>>>Os.listdir (OS.GETCWD ()) A -['1','1.txt','argv.py','Create_utf_8.txt','helloworld.py','Unicode_test.txt'] - the>>> Glob.glob (r'%s\*.py'%os.getcwd ()) - -['e:\\yc_study\\python\\study_imooc\\file_handle\\argv.py','e:\\yc_study\\python\\study_imooc\\file_handle\\helloworld.py'] - +>>> Glob.glob (r'%s\*.txt'%os.getcwd ()) - +['E:\\yc_study\\python\\study_imooc\\file_handle\\1.txt','E:\\yc_study\\python\\study_imooc\\file_handle\\create_utf_8.txt','E:\\yc_study\\python\\study_imooc\\file_handle\\unicode_test.txt']
Glob.glob Use Example
1>>> Glob.iglob (r'%s\*.txt'%os.getcwd ())2 3<generator Object Iglob at 0x000000000256af78>4 5>>> forIinchGlob.iglob (R'%s\*.txt'%OS.GETCWD ()):6 7...PrintI8 9 ...Ten OneE:\yc_study\python\study_imooc\file_handle\1. txt A - E:\yc_study\python\study_imooc\file_handle\create_utf_8.txt - theE:\yc_study\python\study_imooc\file_handle\unicode_test.txt
Glob.iglob Use Example4.python processing various Files 4.1ConfigParse module read configuration file (. ini)
Basic use of &unicode coding issues
Http://www.cnblogs.com/victorwu/p/5762931.html
Deep interpretation
Http://www.cnblogs.com/yyds/p/6627208.html
4.2python Processing of XML files
Http://www.cnblogs.com/yyds/p/6627208.html
Python file Processing--03 file Processing module