Getting the File
tags: #python/documentation/file_handling
Files in a computer operating system are often organized into a hierarchy of folders.
If your file and your Python program are in the same directory you can simply use the filename :
fhandle = open('filename.ext')
If your file and your Python program are in different directories, however, then you need to specify a path.
File path
Relative file path
A relative path refers to a location that is relative to a current directory.
To get a file in a different directory, will need to specify the relative file path, i.e., relative to the directory where the code is running from.
For example, the file samplecode.py could contain the code open('../<foldername>/<file_name>, 'r').
The ../ tells python to go up one level in the directory, to the folder containing the file, if necessary.
Absolute file path
This is the file path on a user's computer from their home directory.
Example:
open('/Users/joebob01/myFiles/allProjects/myData/data2.txt', 'r')
Absolute file paths begins with a /.