Python Forum
Using Python to read external files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Using Python to read external files (/thread-3486.html)



Using Python to read external files - beginnertoday - May-28-2017

Hi, I'm a beginner at coding. I had a query regarding how an external text file (like an .txt file or even a .py file saved on the hard drive of my computer) can be read by Python. My intention is to command Python to read the data stored in that external file and print it. I am unsure of the syntax that should be used to do this.
I attempted to use f = open("filename","r") where filename was the location of the file in my hard drive (i.e. 'C:\User\ and so on). However, this did not work so I attempted to use import sys commands. But nothing seems to be working.

Can anyone help me out? Thanks!! :)


RE: Using Python to read external files - buran - May-28-2017

we have little tutorial on basic file operations, that will help you start

https://python-forum.io/Thread-Basic-Files.

note that backslash \ is used to escape certain special characters. Use forward slash / e.g.
'C:/User/whatever/yourfile.txt'
or
raw string
r'C:\User\whatever\yourfile.txt'


RE: Using Python to read external files - beginnertoday - May-28-2017

Thank you so much!