Files
# create a pipe to a file
>>> file = open(file="my_file.txt", mode="r")
# calling "file" yields the attributes of the file object
>>> file
<_io.TextIOWrapper name="my_file.txt" mode="r" encoding="UTF-8">
# let us source the data
>>> data = file.read()
>>> print(data)
Hi there
# close the pipe
>>> file.close()# the strings (data) to save permanently to a file
>>> FIRSTLAW = "A robot may not injure a human being or, through inaction, "\
"allow a human being to come to harm."
>>> SECONDLAW = "A robot must obey the orders given it by human beings except "\
"where such orders would conflict with the First Law."
>>> THIRDLAW = "A robot must protect its own existence as long as such "\
"protection does not conflict with the First or Second Law."
# create a pipe to a file
>>> file = open(file="my_file.txt", mode="w")
# concatenate the strings
>>> TO_WRITE = "\n".join([FIRSTLAW, SECONDLAW, THIRDLAW])
# write the concatenated strings
>>> file.write(TO_WRITE)
# close the pipe
>>> file.close()# the strings (data) to save permanently to a file
>>> DATA = "The first line\nThe second line"
# create a pipe to a file and write DATA
>>> file = open(file="my_file.txt", mode="w")
>>> file.write(DATA)
>>> file.close()
# read one line from the file
>>> file = open(file="my_file.txt", mode="r")
>>> file.readline()
"The first line\n"
# calling file.readline() again reds the subsequent line
>>> file.readline()
"The second line"
# ... and so on until the end of the file is reached
>>> file.readline()
""# the strings (data) to save permanently to a file
>>> DATA = "A\nB\nC\nD"
# create a pipe to a file and write DATA
>>> file = open(file="my_file.txt", mode="w")
>>> file.write(DATA)
>>> file.close()
# read multiple lines
>>> file = open(file="my_file.txt", mode="r")
>>> file.readlines()
['A\n', 'B\n', 'C\n', 'D']| Method | Description |
|---|---|
file.close() |
Closes the file |
file.detach() |
Returns the separated raw stream from the buffer |
file.fileno() |
Returns a number that represents the stream as per the OS’ perspective |
file.flush() |
Flushes the internal buffer |
file.isatty() |
Returns whether the file stream is interactive or not |
file.read() |
Returns the file content |
file.readable() |
Returns whether the file stream can be read or not |
file.readline() |
Returns one line from the file |
file.readlines() |
Returns a list of lines from the file |
file.seek() |
Change the file position |
file.seekable() |
Returns whether the file allows us to change the file position |
file.tell() |
Returns the current file position |
file.truncate() |
Resizes the file to a specified size |
file.writable() |
Returns whether the file can be written to or not |
file.write() |
Writes the specified string to the file |
file.writelines() |
Writes a list of strings to the file |
Footnotes
For the sake of simplicity, we assume the target file is located in the same directory as the Python script.↩︎
By default,
wtruncates the file if it already exists↩︎If encoding is not specified, the encoding used is platform-dependent. Specifically,
locale.getpreferredencoding(False)is called to get the current locale encoding. Character encoding assigns numbers to graphical characters, especially the written characters of human language, allowing them to be stored, transmitted, and transformed using digital computers.↩︎