0

I have this basic python script to print out the contents of an entire book I have saved as a text file:

f = open("pride.txt", "r", encoding="UTF-8")
s = f.read()
f.close()
print("Contents of file:", s)

But every time I run it, it doesn't actually print out the contents of the book, the output is just:

Contents of file:

What am I doing wrong? I made sure that there is no typo and I am absolutely positive that the text file is saved under the same directory as my program.

martineau
  • 119,623
  • 25
  • 170
  • 301
asvalk1672
  • 105
  • 1
  • 7
  • 1
    The text file may be saved under the same directory as your program, but that doesn't mean it is saved in your working directory which is what relative file paths refer to. – mkrieger1 Jun 20 '22 at 23:21
  • How are you running the file? – Mous Jun 20 '22 at 23:22
  • You might try `os.system("dir")` and make sure you're in the directory you think you are. – Tim Roberts Jun 20 '22 at 23:22
  • 1
    Does this answer your question? [How to reliably open a file in the same directory as the currently running script](https://stackoverflow.com/questions/4060221/how-to-reliably-open-a-file-in-the-same-directory-as-the-currently-running-scrip) – mkrieger1 Jun 20 '22 at 23:23
  • @mkrieger1 what does that exactly mean, your first comment about relative file path – asvalk1672 Jun 20 '22 at 23:26
  • See https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory – mkrieger1 Jun 20 '22 at 23:27
  • It looks like you are opening an empty file. Try `import os;print(os.stat("pride.txt").st_size)` to see if there is anything there. – tdelaney Jun 20 '22 at 23:32
  • Since the open did not raise an exception, we know that there is a file "pride.txt" in the current working directory. You could `print(os.getcwd())` to see if its the directory you expect, but really, this looks like an empty file. Maybe you accidentally opened in "w" mode once while testing. But make sure the file does have something in it. – tdelaney Jun 20 '22 at 23:36
  • I ran that code and got 0 as the output, and now I remember I did actually open in 'w' mode once while testing earlier, but what would that do to the file. I'm just learning about files today so I'm not at all familiar with them. – asvalk1672 Jun 20 '22 at 23:38

1 Answers1

1

Your code looks okay, so to eliminate the possibility that the active directory of the script is not the same as the script'sactual location, give the full path to the file.

Also, it's safer to open files like this:

path = "/full/path/to/the/file/pride.txt"
with open(path, "rt", encoding="UTF-8") as f:
    s = f.read()

print("Contents of file:", s)
Matt Hall
  • 7,614
  • 1
  • 23
  • 36
  • I tried doing that and I got the following error message: `unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape` – asvalk1672 Jun 20 '22 at 23:34
  • @asvalk1672 That's a different problem. See https://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path – mkrieger1 Jun 20 '22 at 23:37
  • ah ok that worked, it printed out all the contents – asvalk1672 Jun 20 '22 at 23:40
  • 1
    ok now my question is, why didn't my original code work? – asvalk1672 Jun 20 '22 at 23:48
  • Your code's 'runtime' (the Python program) has some concept of the current working directory that may or may not line up with what you expect, depending on how you're running Python. Using absolute (i.e. complete) paths is usually your safest bet because there's no ambiguity. – Matt Hall Jun 21 '22 at 00:19
  • If not using an absolute path was the problem an exception saying the file could not be found would have occurred — so it's hard to understand why giving it one would have fixed things (as well as why the OP has accepted your answer). – martineau Jun 21 '22 at 00:41
  • @martineau I have no idea, I guess I was thinking that there may have been some flailing, with an open text editor and multiple versions of the file floating around. I was really just thinking of asking the person to eliminate one possibility, then move on to the next... but SO doesn't really lend itself to dynamic problem solving. Seems like there was a vaguely happy ending at least. – Matt Hall Jun 21 '22 at 01:06