How I can read some random file from my computer? Text or something else?
Question about how to read or write files
Post a Reply
|Oldest Newest Rating|
Otto Von Chesterfield
· August 23, 2015
Yes, you can! You need to use the open() method to do so.
You can also read the whole file in one go!
To write files, you can do:
Tell me if this helps!
· August 24, 2015
There is actually a nice shortcut if you need a relative path and you won't necessarily use the same computer (Or just useful to know nonetheless):
In folder_name you will have the path of the folder that your current code is in.
You can instead import the entire os package, though it is a waste of resources unless you need more functions from it:
To that you can add the relative path of the file that you want to open.
After this you will have to close the file in the end of the use.
A different way to use files in Python is using the command "with". This command leaves the file (Or url) connection open until you are out of the indentation for it and then it closes it (Similar to function use).
This section is all about snakes! Just kidding.
|Bucky Roberts Administrator|