xxxxxxxxxx
txt = open('FILENAME.txt')
txtread = txt.read()
print(txtread)
print(txt.read())
xxxxxxxxxx
# How to read, and print to the screen a file in python!
f = open('fileName', 'r')
print(f.read())
f.close()
# Where "fileName" is obviously the name of your file that you want to read.
xxxxxxxxxx
with open('example.txt', 'r') as file:
contents = file.read()
print(contents)
This code will read the entire contents of the example.txt file and print it to the console.
You can also read the contents of a file line by line using the readline() method. Here's an example:
with open('example.txt', 'r') as file:
line = file.readline()
while line:
print(line)
line = file.readline()
This code will read the example.txt file line by line and print each line to the console.
I hope it will help you. Thank you :)
For more refer link: https://www.programmingquest.com/2023/03/how-to-work-with-files-in-python.html
xxxxxxxxxx
fileName = "file_name.txt" #Here you need to write the file name as a string
openLike = "" #Here you need to write how do you want to open the file:
#"w": Write, "r": Read
openedFile = open("file_name.txt", openLike) #Here you open the file
fileText = openedFile.read() #This read all the file
openedFile.close() #Close the file
print(fileText) # Prints the file text
xxxxxxxxxx
my_file = open("C:\\Users\\Python\\file.txt", "r")
#Give the path accurately and use \\
text = my_file.read()
print(text)
#Output: The text in file.txt will be printed
xxxxxxxxxx
fin = open("NAME.txt", 'r')
body = fin.read().split("\n")
line = fin.readline().strip()
xxxxxxxxxx
lines = []
with open('the-zen-of-python.txt') as f:
lines = f.readlines()
count = 0
for line in lines:
count += 1
print(f'line {count}: {line}')
Code language: JavaScript (javascript)