Copy codeThe Code is as follows: #-*-coding: utf8 -*-
'''
Python common file operation examples
Path Name access function in the OS. path Module
Separate
Basename () removes the directory path and returns the file name.
Dirname () removes the file name and returns the directory path.
Join () combines the separated parts into a path name
Split () returns the (dirname (), basename () tuples.
Splitdrive () returns the (drivename, pathname) tuples
Splitext () returns the (filename, extension) tuples
Information
Getatime () returns the last access time
Getctime () returns the File Creation Time
Getmtime () returns the last file modification time
Getsize () returns the file size (in bytes)
Query
Exists () specifies whether the path (file or directory) exists
Isabs () specifies whether the path is an absolute path
Isdir () specifies whether the path exists and is a directory
Isfile () specifies whether the path exists and is a file
Islink () specifies whether the path exists and is a symbolic link.
Ismount () specifies whether the path exists and is a mount point
Whether two samefile () path names point to the same file
OS. path. isdir (name): determines whether the name is a directory. If the name is not a directory, false is returned.
OS. path. isfile (name): determines whether the name is a file. If the name does not exist, false is returned.
OS. path. exists (name): determines whether a file or directory name exists.
OS. path. getsize (name): Get the file size. If the name is a directory, return 0L
OS. path. abspath (name): Obtain the absolute path.
OS. path. normpath (path): standard path string format
OS. path. split (name): split the file name and directory (in fact, if you fully use the directory, it will also separate the last directory as the file name, and it will not determine whether the file or directory exists)
OS. path. splitext (): separates file names and extensions
OS. path. join (path, name): Connection directory and file name or directory
OS. path. basename (path): returns the file name.
OS. path. dirname (path): Return file path
File Operations in the OS module:
OS module attributes
Linesep is a string used to split rows in a file.
A string used to separate the file path name.
Pathsep is a string used to separate file paths.
String name of the current working directory of curdir
Pardir (current working directory) parent directory string name
1. rename: OS. rename (old, new)
2. Delete: OS. remove (file)
3. List the files in the directory: OS. listdir (path)
4. Get the current working directory: OS. getcwd ()
5. Change the working directory: OS. chdir (newdir)
6. Create a multilevel Directory: OS. makedirs (r "c: \ python \ test ")
7. Create a single directory: OS. mkdir ("test ")
8. Delete multiple directories: OS. removedirs (r "c: \ python") # delete all empty directories in the last directory of the given path.
9. delete a single directory: OS. rmdir ("test ")
10. Get file attributes: OS. stat (file)
11. Modify the file Permission and timestamp: OS. chmod (file)
12. Execute the operating system command: OS. system ("dir ")
13. Start a new process: OS .exe c (), OS .exe cvp ()
14. Execute the program in the background: osspawnv ()
15. Terminate the current process: OS. exit (), OS. _ exit ()
16. Separated file name: OS. path. split (r "c: \ python \ hello. py") --> ("c: \ python", "hello. py ")
17. separation Extension: OS. path. splitext (r "c: \ python \ hello. py ") --> (" c: \ python \ hello ",". py ")
18. Obtain the path name: OS. path. dirname (r "c: \ python \ hello. py") --> "c: \ python"
19. Get the file name: OS. path. basename (r "r: \ python \ hello. py") --> "hello. py"
20. Determine whether the file exists: OS. path. exists (r "c: \ python \ hello. py") --> True
21. Determine whether the path is absolute: OS. path. isabs (r ". \ python \") --> False
22. Check whether the directory is OS. path. isdir (r "c: \ python") --> True
23. Determine whether the file is: OS. path. isfile (r "c: \ python \ hello. py") --> True
24. Determine whether the file is a linked file: OS. path. islink (r "c: \ python \ hello. py") --> False
25. Get file size: OS. path. getsize (filename)
26. *******: OS. ismount ("c: \") --> True
27. Search all files in the directory: OS. path. walk ()
Operations on files by the shutil module:
1. copy a single file: shultil. copy (oldfile, newfle)
2. Copy the entire directory tree: shultil. copytree (r ". \ setup", r ". \ backup ")
3. Delete the entire directory tree: shultil. rmtree (r ". \ backup ")
Temporary File Operations:
1. Create a unique temporary file: tempfile. mktemp () --> filename
2. Open the temporary file: tempfile. TemporaryFile ()
Memory file (StringIO and cStringIO) Operations
[4. StringIO] # cStringIO is a fast implementation module of the StringIO module.
1. Create a memory file and write the initial data: f = StringIO. StringIO ("Hello world! ")
2. read Memory file data: print f. read () # or print f. getvalue () --> Hello world!
3. write Data to memory files: f. write ("Good day! ")
4. close the memory file: f. close ()
'''
Import OS
Import OS. path
Import unittest
Import time
# Import pygame
Class PyFileCommonOperatorTest (unittest. TestCase ):
Def _ init _ (self ):
"" Constructor """
Def test01 (self ):
Print OS. linesep
Print OS. sep
Print OS. pathsep
Print OS. curdir
Print OS. pardir
Print OS. getcwd ()
Print 'unittest here'
If _ name _ = "_ main __":
T = PyFileCommonOperatorTest ()
T. test01 ()
Copy codeThe Code is as follows: # Write a read file:
# Read text files:
Input = open ('data', 'R') # The second parameter is default and can be left empty.
# Read Binary files:
Input = open ('data', 'rb ')
# Read all file content:
Open('xxoo.txt '). read ()
# Read fixed bytes
Open ('abinfile', 'rb'). read (100)
# Read each line
File_object.readlines ()