Python--File directory

Source: Internet
Author: User
Tags create directory

Small Q: The beacon even March, letter to million gold, bald scratching more short, muddy desire to hairpin. ---Du Fu's "spring Look"

-------------------------------------------------------------------------------------------------

    • File Object method: The File object provides a series of methods for manipulating files.

    • OS Object methods: Provides a series of methods for working with files and directories.

File Method ====================================================

Format: File object = open (file_name [, access_mode][, Buffering])
file_name: String value of the file name to access
Access_mode: Open File Mode: read-only, write, append, etc. The default mode is read-only (R).
Buffering: is set to 0, there is no deposit, the value is 1, the row is stored when the file is accessed, and is set to an integer greater than 1.

is the buffer size of the storage area, negative value, the buffer size of the storage area is the system default.

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/E9/wKioL1dEXhDBn40fAADq1wHGxMs716.png "title=" 1.png " alt= "Wkiol1dexhdbn40faadq1whgxms716.png"/>

#!/usr/bin/python#-*-coding:utf-8-*-# Open a file fo = open ("Foo.txt", "WB") print "file name:", Fo.nameprint "is closed:", Fo.clos Edprint "access mode:", Fo.modeprint "at the end of whether to force a space:", fo.softspace>>>>>>>>> file name: Foo.txt is closed: fals E access mode: Whether the end of WB Force plus space: 0
#!/usr/bin/python#-*-coding:utf-8-*-# Open a file fo = open ("Foo.txt", "WB") Fo.write ("Www.runoob.com!\nvery Good site!\n") ; # Close Open File Fo.close () # # #会创建foo. txt file and write the contents to the file and close the file. If you open this file, you will see the following: $ cat foo.txt www.runoob.com!Very Good site!

os file directory =================================================

Python's OS module provides a way to help you perform file processing operations, such as renaming and deleting files.
To use an OS module, you must import it before you can invoke the associated functionality.

Os.mkdir ("file") Create directory
Shutil.copyfile ("Oldfile", "NewFile") Oldfile and NewFile can only be files
Os.rename ("Oldname", "newname") files or directories are all using this command
Shutil.move ("Oldpos", "Newpos") move files (directory)
Os.remove ("file") Delete file
Os.rmdir ("dir") can only delete empty directories
Example: Enter the "/home/newdir" directory?

#!/usr/bin/python#-*-coding:utf-8-*-import OS # Changes the current directory to "/home/newdir" Os.chdir ("/home/newdir") #显示当前目录os. GETCWD ()

Example: Delete the "/tmp/test" directory. The full compliance name of the directory must be given, otherwise the directory will be searched under the current directory

#!/usr/bin/python#-*-coding:utf-8-*-import os # delete "/tmp/test" directory Os.rmdir ("/tmp/test")

Example: Rename an already existing file Test1.txt and delete the file test2.txt

#!/usr/bin/python#-*-coding:utf-8-*-import os # Rename file Test1.txt to Test2.txt. Os.rename ("Test1.txt", "Test2.txt") # Delete a file that already exists Test2.txtos.remove ("Test2.txt")

---------------------------------------------------------------------------------------------------

Note: The specific file, directory method see hyperlinks. ^^^^^^^^


Python--File directory

Related Article

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.