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 mkdir () creates the specified directory Makedirs () Create a multilevel directory GETCWD () to return the current working directory rmdir () Delete the specified directory removedirs () Delete the multilevel directory
(2) Documents:
Mkinfo () Create Pipeline mknod () Create device file Remove () Delete file unlink () Delete link file rename () Rename stat () returns file status information symlink () create symbolic Link utime () update timestamp tmpfile () Create and open (W+B) a new temporary file
(3) Access rights
Access (path, mode) determines whether the specified user has access rights os.access ('/tmp ', 0) UID 0 user has access to/tmp directory chmod (Path,mode ) Modify Permissions os.chmod ('/tmp/s ', 0640) modify/TMP/S permissions to 640 chown (path,uid,gid) modify owner, genus Group umask () Set default permission mode Os.umask (022)
(4) Equipment files
Makedev () Create device major () specify device get master device number minor () specify device get secondary device number
(5) File descriptor
Open () Lower IO opens read () Lower IO reads write () Lower IO Write 4, 5 relative less supplement: Os.walk () equivalent to tree command >>> import os >>> a1 = Os.walk ('/root ') >>> a1.next () ('/root ', ['. Subversion ', '. ssh ', '. Ipython ', '. PKI ', '. Cache '], [' test.py ', '. Bash_history ', '. CSHRC ', '. Bash_logout ', '. TCSHRC ', '. Bash_profile ', '. Mysql_history ', '. BASHRC ', '. Viminfo ']) returns a tuple consisting of (file name, [folder],[file])
The path module in the 2.os module
1) associated with file path
basename () path base name dirname () path directory name join () consolidated filename split () returns DirName (), basename () tuple Splitext () return (filename,extension) tuple Example: >>> dir1 = Os.path.dirname ('/etc/sysconfig/ Iptables-config ') >>> dir1 '/etc/sysconfig ' >>> file1 = Os.path.basename ('/etc/ Sysconfig/iptables-config ') >>> file1 ' iptables-config ' >>> os.path.join (Dir1, file1) '/etc/sysconfig/iptables-config ' >>> for filename in Os.listdir ('/tmp '): print Os.path.join ('/tmp ', filename)
2) Information
Getatime () returns file last accessed time getmtime () returns file last modified getctime () returns the last time the file was changed GetSize ( ) returns the size of the file
3) Query
Exists () determines whether the specified file exists isabs () determines whether the specified path is an absolute path isdir () Whether the directory Isfile ( ) is a file Islink () Whether the symbolic link ismount () is a mount point sanefile (F1,F2) Two paths point to the same file example: Determine if the file exists, open it, Allow users to repeatedly enter multiple rows of data through the keyboard, append to save this file >>> import os >>> import os.path >>> if Os.path.isfile ('/tmp/s '): f1 = open ('/tmp/s ', ' A + ') while True: a2 = raw_input ("Input >>") If a2 = = ' Q ' or a2 = = ' Quit ': Break f1.write (a2+ ' \ n ') F1.close ()
4) Object Persistent storage
The process of changing a variable from memory to a storage or transfer is called serialization Pickle, Marshal, dbm interface, shelve module pickle persisting memory objects stored in file >>> import pickle >>> Dict1 = {' x ': 1, ' Y ': 2, ' z ': ' Hello World '} >>> f1 = open ('/t Mp/s ', ' A + ') >>> pickle.dump (DICT1,F1) Save the dictionary in a file by passing through it >>> F1.close () # file/tmp/s /TMP/S: ASCII Text # cat/tmp/s (dp0 s ' y ' p1 I2 ss ' x ' p2 I1 ss ' z ' P3 s ' Hello World ' P4 S. >>> F2 = open ('/tmp/s ', ' A + ') >>> dict2 = pickle.load (f2) Reload >>> dict2 {' X ': 1, ' Y ': 2, ' z ': ' Hello World '