Python Forum
Using Python to read external files
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Python to read external files
#1
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!! :)
Reply
#2
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'
Reply
#3
Thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 417 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  programming OpenOffice with External Python Project bobthebuilder44 1 773 Apr-07-2023, 06:02 AM
Last Post: buran
  Can I get some clarification on importing functions from external files. wh33t 3 911 Feb-25-2023, 08:07 PM
Last Post: deanhystad
  How to read in mulitple files efficiently garynewport 3 899 Jan-27-2023, 10:44 AM
Last Post: DeaD_EyE
  Read directory listing of files and parse out the highest number? cubangt 5 2,374 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,413 Feb-13-2022, 07:13 PM
Last Post: menator01
  How to send data from a python application to an external application aditya_rajiv 1 2,187 Jul-26-2021, 06:00 AM
Last Post: ndc85430
  Open and read multiple text files and match words kozaizsvemira 3 6,767 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,870 Dec-06-2020, 04:14 PM
Last Post: Divya577
  How to read csv files parallay Mekala 2 2,001 Oct-24-2020, 07:33 AM
Last Post: Mekala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020