Common operations for Python files

Source: Internet
Author: User

[Python]View PlainCopyprint?
  1. #-*-coding:utf8-*-
  2. "' "
  3. Python common file Operations examples
  4. Path name access functions in the Os.path module
  5. Separated
  6. basename () Remove directory path, return file name
  7. DirName () Remove file name, return directory path
  8. Join () combines parts of the separation into one path name
  9. Split () return (DirName (), basename ()) tuple
  10. Splitdrive () return (drivename, pathname) tuple
  11. Splitext () return (filename, extension) tuple
  12. Information
  13. Getatime () returns the last access time
  14. Getctime () returns file creation time
  15. Getmtime () returns the last file modification time
  16. GetSize () returns the file size (in bytes)
  17. Inquire
  18. Exists () specifies whether the path (file or directory) exists
  19. Isabs () Specifies whether the path is an absolute path
  20. Isdir () Specifies whether the path exists and is a directory
  21. Isfile () Specifies whether the path exists and is a file
  22. Islink () Specifies whether the path exists and is a symbolic link
  23. Ismount () Specifies whether the path exists and is a mount point
  24. Samefile () Two path names pointing to the same file
  25. Os.path.isdir (name): Determine if Name is a directory, name is not a directory and return false
  26. Os.path.isfile (name): Determine if name is not a file, does not exist name also returns false
  27. Os.path.exists (name): Determine if there is a file or directory name
  28. Os.path.getsize (name): Get file size, if name is directory return 0L
  29. Os.path.abspath (name): Get absolute path
  30. Os.path.normpath (PATH): Canonical path string form
  31. Os.path.split (name): Split file name and directory (in fact, if you use the directory completely, it will also separate the last directory as the file name, and it will not determine whether the file or directory exists)
  32. Os.path.splitext (): Detach file name and extension
  33. Os.path.join (path,name): Connection directory with file name or directory
  34. Os.path.basename (PATH): Return file name
  35. Os.path.dirname (PATH): Return file path
  36. File operations in the OS module:
  37. OS Module Properties
  38. Linesep string used to separate rows in a file
  39. The string that Sep uses to separate the file path name
  40. Pathsep string used to separate file paths
  41. CurDir the string name of the current working directory
  42. Pardir (current working directory) parent directory string Name
  43. 1. Renaming: Os.rename (old, new)
  44. 2. Delete: Os.remove (file)
  45. 3. List files in directory: Os.listdir (PATH)
  46. 4. Get current working directory: OS.GETCWD ()
  47. 5. Change of working directory: Os.chdir (newdir)
  48. 6. Create a multilevel directory: Os.makedirs (r "C:\python\test")
  49. 7. Create a single directory: Os.mkdir ("Test")
  50. 8. Delete Multiple directories: Os.removedirs (r "C:\python") #删除所给路径最后一个目录下所有空目录.
  51. 9. Delete a single directory: Os.rmdir ("Test")
  52. 10. Get File attributes: Os.stat (file)
  53. 11. Modify file permissions and timestamps: Os.chmod (file)
  54. 12. Execute operating system command: Os.system ("dir")
  55. 13. Start a new process: Os.exec (), OS.EXECVP ()
  56. 14. Executing the program in the background: OSSPAWNV ()
  57. 15. Terminate the current process: Os.exit (), Os._exit ()
  58. 16. Detach file Name: Os.path.split (r "c:\python\hello.py")--("C:\\python", "hello.py")
  59. 17. Detach Extension: Os.path.splitext (r "c:\python\hello.py")--("C:\\python\\hello", ". Py")
  60. 18. Get path name: Os.path.dirname (r "c:\python\hello.py")--"C:\\python"
  61. 19. Get File Name: Os.path.basename (r "r:\python\hello.py")--"hello.py"
  62. 20. Determine if the file exists: os.path.exists (r "c:\python\hello.py")---True
  63. 21. Determine if absolute path: Os.path.isabs (r ". \python\")---False
  64. 22. Determine if it is a directory: Os.path.isdir (r "C:\python")--True
  65. 23. Determine if it is a file: Os.path.isfile (r "c:\python\hello.py")---True
  66. 24. Determine if it is a linked file: Os.path.islink (r "c:\python\hello.py")---False
  67. 25. Get File Size: os.path.getsize (filename)
  68. 26.*******:os.ismount ("c:\\")--True
  69. 27. Search all files in directory: Os.path.walk ()
  70. Shutil the operation of the module to the file:
  71. 1. Copying individual files: shultil.copy (Oldfile, Newfle)
  72. 2. Copy Entire directory tree: Shultil.copytree (r ". \setup", R ". \backup")
  73. 3. Delete entire directory tree: Shultil.rmtree (r ". \backup")
  74. Operations for temporary files:
  75. 1. Create a unique temporary file: tempfile.mktemp ()---filename
  76. 2. Open temporary file: tempfile. Temporaryfile ()
  77. Memory files (Stringio and Cstringio) operations
  78. [4.StringIO] #cStringIO是StringIO模块的快速实现模块
  79. 1. Create the memory file and write the initial data: F = Stringio.stringio ("Hello world!")
  80. 2. Read in memory file data: Print F.read () #或print f.getvalue ()--Hello world!
  81. 3. Want the memory file to write data: F.write ("Good day!")
  82. 4. Close the memory file: F.close ()
  83. ‘‘‘
  84. Import OS
  85. Import Os.path
  86. Import UnitTest
  87. Import time
  88. #import Pygame
  89. Class Pyfilecommonoperatortest (UnitTest. TestCase):
  90. def __init__ (self):
  91. "" " constructor " ""
  92. def test01 (self):
  93. Print Os.linesep
  94. Print Os.sep
  95. Print Os.pathsep
  96. Print Os.curdir
  97. Print Os.pardir
  98. print os.getcwd ()
  99. print ' unittest here '
  100. if __name__ = = "__main__":
  101. t = pyfilecommonoperatortest ()
  102. T.TEST01 ()
[Python]View PlainCopyprint?
[Python]View PlainCopyprint?
  1. #读文件的写法:
  2. #读文本文件:
  3. input = open (' data ', ' R ')#第二个参数是默认的, can not be added
  4. #读二进制文件:
  5. input = open (' data ', ' RB ')
  6. #读取所有文件内容:
  7. Open (' xxoo.txt '). Read ()
  8. #读取固定字节
  9. Open (' abinfile ', ' RB '). Read (+)
  10. #读每行
  11. File_object.readlines ()

Common operations for Python files

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.