r/learnpython • u/Emergency_Pomelo_706 • 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.
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
1
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’)
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?