|
r
|
Open a file for reading only. The file pointer is placed at the beginning of the file.
|
|
w
|
Open a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file.
|
|
a
|
Open a file for appending. The file pointer is placed at the end of the file. If the file does not exist, creates a new file for writing.
|
|
r+
|
Open a file for both reading and writing. The file pointer is placed at the beginning. Overwrites the exsiting content but the new content is shorter than the existing content, the remaining old content will still be present in the file.
|
|
w+
|
Open a file for both reading and writing. Overwrites the file if it exists. If it does not exist, creates a new file for reading and writing.
|
|
a+
|
Open a file for both reading and writing. The file pointer is placed at the end of the file. If it does not exist, creates a new file for reading and writing.
|
|
and so on
|
|
|
\n
|
newline
|
|
\t
|
tab
|
|
\\
|
backslash
|
|
\'
|
single quote
|
|
\"
|
double quote
|
|
\r
|
carriage return
|
readlines(file) |
read a file and returns them as a list of strings. Each element reprensents a line
|
|
readline(file)
|
read a single line from a file and returns it as a string. The line includes the newline character \n
|
|
read(file)
|
read a file and returns it as a single string. The resulting string includes all the characters, including \n
|
|
seek(offset)
|
change the position of the file handle, which is like a cursor that defines from where the data has to be read or written
|
|
next()
|
retrieve the next item from an iterator
|
|
f.write(content)
|
write content to a file.
|
|
f.writelines()
|
write a list of strings to a file
|
f.seek(offset, from_what) offset specifies the number of positions to move, from_what = c(0, 1, 2) 0 (by default): the beginning of a file; 2 : the end of a filef.write() f.writelines they not automatically add \n at the end of the string or the string element of a list# Method 1
with open(file, mode = ' ') as obejct :
...
#Method 2
object = open(file, mode)
object.close()
# open multiple files
with open(file1, "r") as f1, open(file2, "w") as f2:
...
# Iteration of a file using readline() & while loop
with open(file, "r") as filin:
line = filin.readline()
while line != "":
print(line)
line = filin.readline()
# keep a line using readline() & next()
with open('file.txt', 'r') as f:
line1 = f.readline()
next(f)
line3 = f.readline() # return the line after next
# Iteration of a list resturned by readlines()
with open(file, "r") as filin:
lines = filin.readlines() # [s:f] to keep line
for line in lines:
print(line)
# Iteration directly for a file
# could return error, specially too many strings
# but much more efficient than readlines()
with open(file, "r") as filin:
for line in filin:
print(line)
# change the position
with open(file, 'r') as f:
f.seek(20)
print(f.tell()) # Prints the new position
with open(file, 'r') as f:
f.seek(-10, 2)
# writing
with open(file, "w") as filout:
for element in my_list:
filout.write(f"{element}\n")
# \r
import time
total = 10
for i in range(total + 1):
progress = i / total * 100
bar = '#' * i + '-' * (total - i)
print(f'Progress: [{bar}] {progress:.1f}%', end='\r')
time.sleep(0.5)
print('\nDone!')
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.