Can we import txt file in this app in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can we import txt file in this app in python?

If yes, then how??

9th Jul 2020, 10:10 AM
Kiran Bhingare
Kiran Bhingare - avatar
3 Answers
+ 2
yes with open('text_file.txt') as file: text = file.read() print(text) # make sure youre in the right directory
9th Jul 2020, 10:28 AM
Slick
Slick - avatar
+ 5
If you need to do something more than printing the file content, you can read the file line by line, stripping away the "newline" sequence, and putting it then in a list: lines = [] for i in open('txt3.txt','r'): # file will be opened for reading lines.append(i.strip()) # read line by line and append it to a list for j in lines: print(j)
9th Jul 2020, 11:06 AM
Lothar
Lothar - avatar
+ 2
Not without already having it in your code.
9th Jul 2020, 10:31 AM
Seb TheS
Seb TheS - avatar