I’ve been writing up my notes over the past few days, this being one such note in my files. It kind of speaks for itself, but if someone could please review this and let me know if what I have here is correct and suggest any improvements.
I’m building my own library of functions and useful code, so that I don’t have to ‘reinvent the wheel’ each time I start a new project.
I know that this is not a short read and as such is not a small ask, but the other side of the coin is that it may help others who are, like me, less experienced.
Thanks.
Handling files
Avoid using os.chdir
and relative paths (remembering PEP 20: Explicit is better than implicit.). Instead construct the full (absolute) paths:
#!/usr/bin/python3
import os.path
#---------------------------------------------------#
def process_file(filename, path=None):
if path is not None:
filename = os.path.join(path, filename)
return(filename)
#---------------------------------------------------#
path = '/home/rob/Desktop'
file = 'days.txt'
f = open((process_file(file, path)), 'r')
output = f.read()
print(output)
f.close()
File processing takes place in the following order:
-
Open a file that returns a file object (fileHandle)
-
Use the fileHandle to perform read or write actions
-
Close the file
Open a file
file syntax
file_object = open(file_name, access_mode)
There are six access modes:
-
'r'
Read only: the default -
'w'
Write: Create a new file or overwrite the contents of an existing file. -
'a'
Append: Write data to the end of an existing file. -
'r+'
Both read and write to an existing file. The file pointer will be at the beginning of the file. -
'w+'
Both read and write to a new file or overwrite the contents of an existing file. -
'a+'
Both read and write to an existing file or create a new file. The file pointer will be at the end of the file.
Read a file
file syntax
file_object.read()
There are three functions with which we can read the data in a file.
-
.read()
- By default, returns all the characters in a file, until EOF or until the value of
B
.- Can be used in a loop function to read a set number of characters or bytes.
- By default, returns all the characters in a file, until EOF or until the value of
-
.readline()
- By default, returns all the characters up to EOL (e.g ‘\n’) or until the value of
B
.- Can be used in a loop function to read a set number of lines.
- By default, returns all the characters up to EOL (e.g ‘\n’) or until the value of
-
.readlines()
- By default, returns all the lines in a file in the format of a
list
where each element is a line in the file.- Can be used in a loop to read a set number of lines.
- By default, returns all the lines in a file in the format of a
The constructor (B)
can be used to set the maximum number of bytes (where B
is a positive integer value) to be returned for any of the above (except .readlines()
), by using the file pointer as a ‘stop’, starting again with the next character, if another ‘read’ is called before the working file is closed.
With .readlines()
the file pointer, as set by the value of B
, increments to the next \n
position, rather than the next character. Any value of B
that is less than the position of the next \n
is ignored.
If B
is omitted or has the value of zero, then the function will perform its default action.
The file pointer will not be reset until the working file has been closed and will not read past the EOF.
Write a file
file syntax
file_object.write(<some_data>)
A method to write data to a file. Existing data may or may not be overwritten depending on the access mode used when said file was opened.
Said file may also be protected by the OS if it has a ‘read-only’ attribute set.
<some_data> could be anything from a single byte to multiple lines of text, formatted any way you choose.
Close a file
file syntax
file_object.close()