Relative and absolute file paths
- when you pass a filename like pid_digits.txt to Path, Python checks the directory where the file that’s currently being executed is stored
- sometimes the file you want to open is stored in a different directory
- for example, you might store your program files in a folder called python_work; inside python_work, you might have a folder called text_files to distinguish your program files from the text files they’re manipulating
- even though text_files is in python_work, passing Path the name of a file in text_files won’t work, since Python will only look in python_work
- need to provide the correct path to get Python to open files from a directory other than the one where your program file is stored
- two ways to specify paths in programming
- relative file path – tells Python to look for a given location relative to the directory where the current program file is stored
- since text_files is in python_work, we need to build a path that starts with the directory text_files, and ends with the filename
path = Path(‘text_files/filename.txt’)
- absolute file path – tell Python exactly where the file is on your computer, regardless of where the current program running is stored
- alternative if a relative path doesn’t work
- for example, if text_files is in a folder that’s not python_work, then passing Path ‘text_files/filename.txt’ won’t work since Python is only checking inside python_work
- absolute paths are longer since they start at your system’s root folder
path = Path(‘/home/eric/data_files/text_files/filename.txt’)
- for now, store files in the same directory as your program files, or in a folder such as text_files within the directory that stores your program files
Accessing a file’s lines
- you’ll often want to examine each line in a file
- for example, you might want to read through a file of weather data and work with any line that includes the word sunny
- or in a news report, you would look for a line with the tag <headline> and rewrite that line with a specific kind of formatting
- splitlines() method – to turn a long string into a set of lines, then use a for loop to examine each line from a file
file_reader.py
from pathlib import Path
path = Path(‘pi_digits.txt’)
contents = path.read_text()
lines = contents.splitlines()
for fine in lines:
….print(line)
- we start out by reading the entire contents of the file
- if you’re planning to work with the individual lines in a file, you don’t need to strip any whitespace when reading the file
- splitlines() method returns a list of all lines in the file
- then we assign this list to the variable lines
- then loop over these lines and print each one
3.1415926535
8979323846
2643383279
- since we haven’t modified any of the lines, the output matches the original text file exactly
Working with a file’s contents
- after you’ve read the contents of a file into memory, you can do whatever you want with the data
- first, we’ll attempt to build a single string containing all the digits with no whitespace
pi_string.py
from pathlib import Path
path = Path(‘pi_digits.txt’)
contents = path.read_text()
lines = contents.splitlines()
pi_string = “
for line in lines:
….pi_string += line
print(pi_string)
print(len(pi_string))
- we start by reading the file and storing each line of digits in a list
- then create a variable, pi_string, to hold the digits of pi
- we write a loop that adds each line of digits to pi_string
- we print this string, and also show how long the string
3.1415926536 8979323846 264338379
36
- variable pi_string contains the whitespace that was on the left side of the digits in each line, but we can use lstrip()
–snip–
for line in lines:
….pi_string += line.lstrip()
print(pi_string)
print(len(pi_string))
- now we have a string containing pi to 30 decimal places
- string is 32 characters long because it also includes the leading 3 and a decimal point
3.141592653589793238462643383279
32
- Python interprets all text in a text file as a string, if you read in a number and want to work with that value in a numerical context, you’ll have to convert it to an integer using the int() function or a float using the float() function
End of study session.