Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
Thread: Opening and reading a text file
12-07-2013, 06:22 PM #1
- Join Date
- Sep 2010
- Thanked 0 Times in 0 Posts
Opening and reading a text file
I am a relatively new beginner trying to learn Python and been trying to open and read an ordinary text file from the command line.
I open a terminal and type python and then at the prompt i type:
text_file = open("filename.py", "r") text_file.close()
12-08-2013, 06:35 PM #2
- Join Date
- Jan 2011
- Thanked 605 Times in 604 Posts
You have opened a file and closed a file, you never read the file.
puts the file in variable output.Code:output = text_file.read()
FYI, I hate doing things in the command line. If you rather have a GUI try IDLE. It free and nice when your starting out.
Evolution - The non-random survival of random variants.
"If you leave hydrogen alone, for long enough, it begins to think about itself."
03-21-2014, 11:35 AM #3
I heard this way is better:
Code:with open("path_to_file") as f: text_file = f.read()
I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
** Independent Multimedia Artist in Pasig **