Open Function
You must first open a file with the Python built-in open () function, create a Document object, and the associated helper method can call it for read and write.
Grammar:
File Object = open (file_name [, access_mode][, Buffering])
The details of each parameter are as follows:
The File_name:file_name variable is a string value that contains the name of the file you want to access.
Access_mode:access_mode determines the mode of opening the file: read-only, write, append, etc. All the desirable values are shown in the full list below. This parameter is non-mandatory and the default file access mode is read-only (R).
Buffering: If the value of buffering is set to 0, there is no deposit. If the value of buffering is 1, the row is stored when the file is accessed. If you set the value of buffering to an integer greater than 1, it indicates that this is the buffer size of the storage area. If a negative value is taken, the buffer size of the storage area is the system default.
Full list of open files in different modes:
Properties of the File object
After a file is opened, you have a files object that you can get various information about the file.
The following is a list of all properties related to the file object:
The following example:
#!/usr/bin/python#-*-coding:utf-8-*-# Open a file fo = open ("Foo.txt", "WB") print "Name of the file:", Fo.nameprint "Close D or not: ", Fo.closedprint" Opening mode: ", Fo.modeprint" Softspace flag: ", Fo.softspace
The result of the above example output:
Name of the file:foo.txtClosed or not:falseopening mode:wbsoftspace flag:0
Close () method
The close () method of the file object flushes any information that has not yet been written in the buffer and closes the file, which can no longer be written.
When a reference to a file object is re-assigned to another file, Python closes the previous file. Closing a file with the close () method is a good habit.
Grammar:
Fileobject.close ();
Example:
#!/usr/bin/python#-*-coding:utf-8-*-# Open a file fo = open ("Foo.txt", "WB") print "Name of the file:", Fo.name # Close open files fo . Close ()
The result of the above example output:
Name of the File:foo.txt
Read and Write files:
The file object provides a series of methods that make it easier to access our files. Take a look at how to read and write to the file using the read () and write () methods.
Write () method
The write () method writes any string to an open file. It is important to note that the Python string can be binary data, not just text.
The Write () method does not add a line break (' \ n ') at the end of the string:
Grammar:
Fileobject.write (string);
Here, the parameter being passed is the content to be written to the open file.
Example: #!/usr/bin/python#-*-coding:utf-8-*-# Open a file fo = open ("/tmp/foo.txt", "WB") fo.write ("Python is a great language.\ Nyeah its great!! \ n "); # Close Open File Fo.close ()
The above method creates a Foo.txt file, writes the received content to the file, and eventually closes the file. If you open this file, you will see the following:
Python is a great language. Yeah its great!!
Read () method
The Read () method reads a string from an open file. It is important to note that the Python string can be binary data, not just text.
Grammar:
Fileobject.read ([count]);
Here, the parameter being passed is the count of bytes to read from the open file. The method reads in from the beginning of the file, and if it does not pass in count, it tries to read as much more content as possible, most likely until the end of the file.
Example:
Just use the file Foo.txt we created above.
#!/usr/bin/python#-*-coding:utf-8-*-# Open a file fo = open ("/tmp/foo.txt", "r+") str = Fo.read (Ten);p rint "Read String is: ", str# close Open file Fo.close ()
The result of the above example output:
Read String Is:python is
File Location:
The tell () method tells you the current position within the file, in other words, the next read and write occurs after so many bytes at the beginning of the file:
The Seek (offset [, from]) method changes the position of the current file. The offset variable represents the number of bytes to move. The from variable specifies the reference position at which to begin moving bytes.
If from is set to 0, this means that the beginning of the file is used as the reference location for moving bytes. If set to 1, the current position is used as the reference location. If it is set to 2, then the end of the file will be used as the reference location.
Example:
Just use the file Foo.txt we created above.
#!/usr/bin/python#-*-coding:utf-8-*-# Open a file fo = open ("/tmp/foo.txt", "r+") str = Fo.read (Ten);p rint "Read String is: ", Str # finds the current position position = Fo.tell ();p rint" present file Position: ", Position # reposition the pointer again to the beginning of the file position = Fo.seek (0, 0); St r = Fo.read;p rint "Again read String is:", str# close Open file Fo.close ()
The result of the above example output:
Read string Is:python iscurrent file Position:10again Read string Is:python is
Renaming and deleting files
Python's OS module provides a way to help you perform file processing operations, such as renaming and deleting files.
To use this module, you must first import it, and then you can invoke the various functions associated with it.
Rename () Method:
The rename () method requires two parameters, the current file name, and a new filename.
Grammar:
Os.rename (Current_file_name, New_file_name)
Example:
The following example renames a file that already exists test1.txt.
#!/usr/bin/python#-*-coding:utf-8-*-import os # Rename file Test1.txt to Test2.txt. Os.rename ("Test1.txt", "Test2.txt")
Remove () method
You can delete the file using the Remove () method, and you need to provide the filename to delete as a parameter.
Grammar:
Os.remove (file_name)
Example:
The following example deletes a file that already exists test2.txt.
#!/usr/bin/python#-*-coding:utf-8-*-import OS # Delete a file that already exists Test2.txtos.remove ("Text2.txt")