About
How to manipulate a file. See also: Python - File System
You can extract the content from a file object only once; if you want to read the file again, you need to create a new file object by calling open again.
Articles Related
Syntax
variable = open("path", "mode")
where
- path is the path
- mode can be:
- w : write-only mode (“w”),
- r : read-only mode (“r”),
- r+ : read and write mode (“r+”),
- a : append mode (“a”, which adds any new data you write to the file to the end of the file).
Mode
Modes | Description |
---|---|
r | Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. |
rb | Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. |
r+ | Opens a file for both reading and writing. The file pointer will be at the beginning of the file. |
rb+ | Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file. |
w | Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
wb | Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
w+ | Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. |
wb+ | Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. |
a | Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. |
ab | Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. |
a+ | Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. |
ab+ | Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. |
Path
Management
File extension
filename, file_extension = os.path.splitext('/path/to/somefile.ext')
print filename
print file_extension
.ext
/path/to/somefile
Write
my_list = [i**2 for i in range(1,11)]
# Generates a list of squares of the numbers 1 - 10
f = open("output.txt", "w")
for item in my_list:
f.write("Data to be written")
f.write(str(item) + "\n")
f.close()
Read
- Read function:
my_file = open("output.txt","r")
print my_file.read()
my_file.close()
The Read function will clear the buffer!
print "First Read: ", my_file.read()
print "Second Read: ", my_file.read()
First Read: The first line of my file
Second Read:
- for line in
f = open('stories_big.txt')
for line in f:
print(line)
- List: If the file is not too big, use list(�) to directly obtain a list of the lines in the file.
f = open('stories_small.txt')
stories = list(f)
len(stories)
Empty
os.stat("file").st_size == 0
Close
During the I/O process, data are buffered.
Python doesn't flush the buffer (write data to the file) until you close the file. If you write to a file without closing, the data won't make it to the target file.
Auto-Close
file objects contain a special pair of built-in methods:
- enter()
- and exit().
When a file object's exit() method is invoked, it automatically closes the file.
The method is invoked with “with and as”.
with open("text.txt", "w") as my_file:
my_file.write("Success!")
Closed ?
if my_file.closed == False:
my_file.close()
print my_file.closed