Which files are used to read, write in python files modules? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Which files are used to read, write in python files modules?

I don't understand which types of files are used in python? I have many .txt files but not able to access through the code. How to add and open file?

21st Jul 2020, 1:57 PM
Rushit Soni
Rushit Soni - avatar
2 Answers
+ 3
Any type of files can be used. You can handle program files, media files, data files and more.
21st Jul 2020, 2:02 PM
Seb TheS
Seb TheS - avatar
+ 3
To open files you can use with statement: with open(file, mode) as foo: ... where file represents the file name (with the dot extension) and mode represents the way of handling the file. foo is a variable which refers to the file. To read the file you need to set the mode to "r" if you want to read the file as text, use "rb" if you want to read the file as bytes. you can then read the file contents using the read method: with open(file, "r") as f: content = f.read() print(content)
21st Jul 2020, 2:15 PM
Seb TheS
Seb TheS - avatar