Opening text files inside pycharm | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Opening text files inside pycharm

I have a text file (untitled.txt for example) I put it inside the same working file as my python document I’m working on (texteditor.py lets say). I want to have python open the text file so I can mess w it. It’s a long list of names. Like so - NameA nameB nameC nameD NameE nameF nameG nameH What I want to do is open the text document and fine some way to have python rearrange the names in a predictable order. But I can’t even open the file. I have looked up multiple commands but python keeps saying it can’t find the file when I use the open file command or anything like that. Help!

31st Mar 2019, 4:48 AM
Timothy Hatfield II
Timothy Hatfield II - avatar
1 Answer
+ 2
Hello Timothy this is a simple example that works even in Sololearn, although the created file is immediately forgotten after the program finishes. You best try it on your PC so you can verify the txt files in the end. Tested in Pycharm too :) https://code.sololearn.com/cNEffeZMoXV7/#py It shows how you can open files in write or read mode with context manager. As a beginner warning, just be careful if you open something with w flag, it will be truncated and there is no way to restore the original contents.
3rd Apr 2019, 1:31 PM
Tibor Santa
Tibor Santa - avatar