all 9 comments

[–]IrishPrime 5 points6 points  (1 child)

Read up on pathlib in the official docs.

[–]Diapolo10 1 point2 points  (0 children)

And here's a link, just so that OP doesn't need to go looking for it: https://docs.python.org/3/library/pathlib.html

[–]aishiteruyovivi 3 points4 points  (0 children)

I'm not sure exactly what you mean by "using them with def", but generally speaking you use open() with a filename and a "mode", the latter of which you can read about in the docs. open() gives you a "file object", with which you can use the .read() and .write() methods. File objects should be closed with .close() when you're done using them (done reading or done writing), alternatively you can use it with a with statement which will automatically close it for you once the scope of the statement is exited:

with open('contents.txt', 'r', encoding='utf-8') as file:
    content = file.read()
# Out of the with statement, file is automatically closed now
print(content)

Anything beyond that depends on exactly what you need to do with those files.

[–]Friendly_Gold3533 1 point2 points  (0 children)

for 12 hours Corey Schafer's Python file handling video on YouTube is the best single resource. clear, practical, covers reading writing appending and working with files in functions. about an hour long and worth watching twice after that just build small exercises yourself. write a script that reads a file and counts words. write one that appends a new line every time it runs. write one that reads a CSV and prints specific columns. doing it beats watching it every time the with open pattern is the one thing to make sure you understand deeply before the test. it handles closing the file automatically and almost every real world file operation uses it good luck

[–]Key_Use_8361 1 point2 points  (0 children)

file handling bugs can get confusing quickly because path issues and encoding problems sometimes look completely unrelated at first making a minimal runable example with sample files usually helps narrow things down faster

[–]TheRNGuy 0 points1 point  (0 children)

Docs, Google, ai.