[Tutor] reading files in Python 3

Rafael Knuth rafael.knuth at gmail.com
Thu Mar 30 13:02:13 EDT 2017


I can read files like this (relative path):

with open("Testfile_B.txt") as file_object:
    contents = file_object.read()
    print(contents)

But how do I read files if I want to specify the location (absolute path):

file_path = "C:\Users\Rafael\Testfile.txt"
with open(file_path) as file_object:
    contents = file_object.read()
    print(contents)

The above does not work ...


More information about the Tutor mailing list