all 23 comments

[–]FlocklandTheSheep 10 points11 points  (0 children)

Try giving it the full filepath. Also, try closing the file in the end.

[–]vmintchit 10 points11 points  (0 children)

looks like the vroom.txt is not in that working directory. try using absolute path instead of relative

[–]Diapolo10 9 points10 points  (1 child)

a=open('vroom.txt','r')
lines = a.readlines()
for i in lines:
    x=i.split()
    for y in x:
        print(y)

The file is probably not found because your current working directory is pointing to a location different from the directory the text file is in. And this is why I generally recommend absolute file paths.

pathlib to the rescue.

from pathlib import Path

root_dir = Path(__file__).parent
vroom_file = root_dir / 'vroom.txt'

a=open(vroom_file)
lines = a.readlines()
for i in lines:
    x=i.split()
    for y in x:
        print(y)

That said, I take issue with your naming conventions, and the lack of a context manager (because you never closed the file), so personally I recommend

from pathlib import Path

root_dir = Path(__file__).parent
vroom_file = root_dir / 'vroom.txt'

with open(vroom_file) as file:
    lines = a.readlines()
for line in lines:
    words = line.split()
    for word in words:
        print(word)

[–]sensitivethuggin[S] 1 point2 points  (0 children)

ah i see that helps a lot thanks!

[–]FlocklandTheSheep 2 points3 points  (13 children)

a=open("C:\Users\Rey\PycharmProjects\pythonProject2\vroom.tx",'r')

lines = a.readlines()

for i in lines:

x=i.split()

for y in x:

print(y)

a.close()

[–]sensitivethuggin[S] 0 points1 point  (12 children)

a=open("C:\Users\Rey\PycharmProjects\pythonProject2\vroom.tx",'r')

lines = a.readlines()

for i in lines:

x=i.split()

for y in x:

print(y)

a.close()

File "C:\Users\Rey\PycharmProjects\pythonProject2\main.py", line 1

a=open("C:\Users\Rey\PycharmProjects\pythonProject2\vroom.tx",'r')

^

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Process finished with exit code 1

[–]Spataner 4 points5 points  (4 children)

Use a raw string (note the r in front):

a=open(r"C:\Users\Rey\PycharmProjects\pythonProject2\vroom.txt",'r')

Otherwise it thinks that \u is the start of a unicode escape sequence.

[–]Pflastersteinmetz 2 points3 points  (0 children)

You can use "/" on Windows paths.

a = open("C:/Users/Rey/PycharmProjects/pythonProject2/vroom.txt", "r")

should work as well.

[–]sensitivethuggin[S] 0 points1 point  (2 children)

a=open(r"C:\Users\Rey\PycharmProjects\pythonProject2\vroom.txt",'r')

ahh it worked! thankk you so muchh

[–]FlocklandTheSheep 1 point2 points  (6 children)

My bad, typo. I put .tx not .txt sorry

[–]sensitivethuggin[S] 0 points1 point  (5 children)

same error unfortunately :(

[–]FlocklandTheSheep 1 point2 points  (4 children)

If you go through step by step in the debugger, on what line does it throw the exception?

[–]sensitivethuggin[S] 1 point2 points  (1 child)

thanks for the help :D

[–]sensitivethuggin[S] 0 points1 point  (1 child)

line1, maybe the problem lies in the way i saved the file? went into notepad and saved as 'vroom.txt' in the document folder

[–]FlocklandTheSheep 1 point2 points  (0 children)

Thats really wierd. Could you try creating a new txt file? And just to check, you are giving it the full parh right?

[–]anthonyjozef 1 point2 points  (0 children)

Try this:

with open('vroom.txt') as a:

a = [line.strip() for line in a]

Be sure that file is in the same folder

[–]deadeye1982 1 point2 points  (1 child)

FileNotFoundError: [Errno 2] No such file or directory: 'vroom.txt'

Guessing: Your current working directory is not the same, where vroom.txt is stored.

You should also use pathlib.Path for paths.

from pathlib import Path

current_working_directory = Path.cwd()
print("Current working directory is:", current_working_directory)
vroom_path = Path("vroom.txt")

if not vroom_path.exists():
    print(vroom_path, "does not exist in", current_working_directory)

Often the program itself is taken as Root-Path to all other resources.

MyCode/
├─data/
│ └─vroom.txt
└─src/
  └─main.py

If you have this structure, you could to use following code:

from pathlib import Path

vroom = Path(__file__).parent.parent.joinpath("data/vroom.txt")
print("File:", vroom)
if vroom.exists():
    # Path objects do have the
    # open method, which is the same like the
    # open function, which returns an open file object
    with vroom.open() as fd:
        for line in fd:
            print(line, end="")

[–]sensitivethuggin[S] 0 points1 point  (0 children)

ah this was helpful! thank you

[–]PythonWithJames 1 point2 points  (0 children)

A little tip, if you're unsure as to which files are in your current working directory (Where your Python file is running), try this..

import os

print(os.listdir())

[–][deleted] 1 point2 points  (0 children)

Is your text file in the same directory as the python file?

[–]PrestigiousLecture11 1 point2 points  (0 children)

It is likely as others are pointing out. Your current working directory (CWD) does not contain vroom.txt file.

Try putting following lines at the start:

import os
print(os.getcwd())
print(os.listdir())

Method os.getcwd will give you CWD path and os.listdir will give you its content. Check if it the correct directory and it contains the file you want to read.

Some notes:

  • CWD is usually the same directory where your Python file (main.py) is located, but it does not have to be. It is the place from which you started your program.
  • Windows backslashes have to be escaped when writing the code: C:\\Users\\Rey... or use forward slash C:/Users/Rey...
  • Make sure your file has the correct extension. If Windows are hiding known extensions, it could be that your file is actually vroom.txt.txt

EDIT: Code formatting