Skip to content

11.1/11.2 Locating and Working with Files

11.1 Working With Data Files

  • In Python, we must open files before we can use them and close them when we are done with them
  • Once a file is opened it becomes a Python object just like all other data

Pasted image 20230519154538.png

11.2 Finding a File on your Disk

Pasted image 20230519154525.png

  • Here’s the important rule to remember: If your file and your Python program are in the same directory you can simply use the filename like this: open('myfile.txt', 'r')
  • If your file and your Python program are in different directories then you must refer to one or more directories:
    • in a relative file path to the file like this: open('../myData/data3.txt', 'r')
    • or in an absolute file path like open('/users/bmiller/myFiles/allProjects/myData/data3.txt', 'r')