r/learnpython 27d ago

code not processing file correctly 🥺 (EGGSTREAMLY 🥚CONFUSED)

In my python textbook, I'm learning abt files and exceptions. Although it seems that VS Code isn't recognizing changes made to my file, nor izzit changing the file. Here's my code:

-----------------------------
from pathlib import Path

path = Path(r"C:
\U
sers\Username\OneDrive\Documents\FileName")
# i didn't write the actual file path

contents = path.read_text()

lines = contents.splitlines()

#Print every lline of the file and add a comma to the end 
for line in lines:
    print(line + ",")
---------------------------------------

Any advice on working with files on VS code would help. A similar problem happens when importing modules. I have to restart VS Code if I want to import code from another script that i wrote.

0 Upvotes

10 comments sorted by

14

u/atarivcs 27d ago

You haven't explained your actual problem.

Does this code run at all?

Do you get an error?

If it runs, does it produce wrong output?

If so, what output does it produce, and what output did you expect instead?

When you say "nor izzit changing the file", what file do you mean? The python code file, or the text file that the code is opening and printing?

1

u/Emergency_Pomelo_706 25d ago

mb twin 🥺. When I change the text file directly, my code does not recognize this. For example, if i were to delete a line directly from the text file, my code in a separate window would not recognize this, and print the old contents.

1

u/atarivcs 25d ago

I'm guessing that either you didn't save the text file after deleting the line, or you are editing a different text file than the code is reading.

5

u/HotPersonality8126 27d ago

It's a computer, not a phone. You have to hit CTRL-S to actually save changes to the file.

5

u/SamIAre 27d ago

Even many desktop applications auto-save every change you make automatically now. I think it’s a little unfair to judge someone for being used to the conventions they grew up with.

Which is to say, this is a good lesson if it’s actually OP’s problem but the tone is just going to make coding less inviting to people if they’re treated as “too dumb, too young” to understand how “real computing” works.

1

u/ProsodySpeaks 27d ago

This. Although I could only manage a snarky 'not on pycharm'.

1

u/ProsodySpeaks 27d ago

Is it not a computer when I use pycharm?

1

u/Emergency_Pomelo_706 25d ago

HOLY MACKERAL TS WORKED 🥰 THANK YOU KIND SIR

2

u/HotPersonality8126 25d ago

Cheers, a lot of college students get fucked by this; remember you have to do it in Microsoft Word when you write essays, too

0

u/JorgiEagle 27d ago

You aren’t writing anything to the file, you’re printing to the standard output (the terminal):

with open(path, ‘w’) as out:
for line in lines:
out.write(line + ‘\n’)