Python File System Function: os Module
1. Classification of OS Module Method
(1) Catalogue:
chdir() changes the working directory chroot() sets the root directory of the current process listdir() lists all file names in the specified directory mkdir() Creates the specified directory makedirs() Create multilevel directories getcwd() returns to the current working directory rmdir() Deletes the specified directory removedirs() deletes multilevel directories
(2) Documents:
mkinfo() creates pipes mknod() Creates device files remove() delete files unlink() deletes linked files rename() rename stat() returns file status information symlink() creates symbolic links utime() update timestamp tmpfile() creates and opens (w+b) a new temporary file
(3) Access privileges
access(path, mode) determines whether a specified user has access to os. access ('/tmp', 0) uid of 0 and whether a user has access to the / tmp directory chmod(path,mode) modifies the permission os.chmod('/tmp/s',0640) to change the permission / tmp/s to 640 chown(path,uid,gid) modifies ownership and group umask() sets the default permission mode os.umask(022)
(4) Equipment documents
makedev() create device major() Specifies the device to get the master device number minor() specifies the device to get the secondary device number
(5) File descriptors
open() Lower IO Open read() Lower IO reading write() Lower IO write 4 and 5 are relatively less used Supplement: os.walk() is equivalent to the 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])
2. path module in OS module
1) Related to file path
basename() Path base name dirname() Path directory name join() Consolidate filenames split() Return 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 the last access time to the file getmtime() returns the last modification time of the file getctime() returns the last time the file was changed getsize() returns the size of the file
3) Query
exists() Determine whether the specified file exists isabs() Determine whether the specified path is an absolute path isdir() Is it a directory? isfile() Is it a document? islink() Whether symbolic links exist or not ismount() Is it a mount point? sanefile(f1,f2) Does the two paths point to the same file? //Example: To judge whether a file exists or not, open it, let the user input multiple lines of data repeatedly through the keyboard, and save it to 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 variables from memory to storage or transmission is called serialization. pickle,marshal,DBM Interface, shelve Modular pickle Persistent storage of memory objects in files >>> import pickle >>> dict1 = {'x':1,'y':2,'z':'hello world'} >>> f1 = open('/tmp/s','a+') >>> pickle.dump(dict1,f1) Keep dictionaries in files by passing >>> 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) Reloading >>> dict2 {'x':1,'y':2,'z':'hello world'}