Note that this line of code can leave the file open for reading. Try using a with statement.
f = open('myfile.txt')
Reference: What is the python “with” statement designed for?
The other answers are not wrong. I wanted to add a solution with the recently introduced assignment expression "walrus operator" in Python 3.8.
Reference: Assignment Expressions: The Walrus Operator
def read_file():
chunk_size = int(input("How many letters do you want to read each time?: "))
with open('myfile.txt') as fh:
while (contents := fh.read(chunk_size)):
print(contents)
read_file()
Output:
to be or
not to
be that
is the q
uestion
Answer from dmmfll on Stack OverflowHow to read x characters at a time from a file in python? - Stack Overflow
count - Find the number of characters in a file using Python - Stack Overflow
How can you read a specific number of characters from a file in Python?
Select one:
A. Use the read() function with a parameter specifying the number of characters to read
B. Use the readlines() function with a parameter specifying the number of characters to read
python - Read N lines from a file - Stack Overflow
Hey there, I want to read a file with \n characters, but instead of giving newlines, it prints the raw character.
for example:
in main.py:
with open("text.txt", "r") as f:
output_text = f.read()
print(output_text)in text.txt
this text contains newline characters \n and i want to print them as newlines \n this should be on another line
the output: this text contains newline characters \n and i want to print them as newlines \n this should be on another line
How do i make it so that it outputs like this?
this text contains newline characters and i want to print them as newlines this should be on another line
Note that this line of code can leave the file open for reading. Try using a with statement.
f = open('myfile.txt')
Reference: What is the python “with” statement designed for?
The other answers are not wrong. I wanted to add a solution with the recently introduced assignment expression "walrus operator" in Python 3.8.
Reference: Assignment Expressions: The Walrus Operator
def read_file():
chunk_size = int(input("How many letters do you want to read each time?: "))
with open('myfile.txt') as fh:
while (contents := fh.read(chunk_size)):
print(contents)
read_file()
Output:
to be or
not to
be that
is the q
uestion
You need a loop around the reading/printing part:
def read_file():
x = int(input("How many letters do you want to read each time : ")) # number of characters to read on each newline
f = open('myfile.txt')
while True:
contents = f.read(x)
if not contents:
break
print(contents)
Sum up the length of all words in a line:
characters += sum(len(word) for word in wordslist)
The whole program:
with open('my_words.txt') as infile:
lines=0
words=0
characters=0
for line in infile:
wordslist=line.split()
lines=lines+1
words=words+len(wordslist)
characters += sum(len(word) for word in wordslist)
print(lines)
print(words)
print(characters)
Output:
3
13
35
This:
(len(word) for word in wordslist)
is a generator expression. It is essentially a loop in one line that produces the length of each word. We feed these lengths directly to sum:
sum(len(word) for word in wordslist)
Improved version
This version takes advantage of enumerate, so you save two lines of code, while keeping the readability:
with open('my_words.txt') as infile:
words = 0
characters = 0
for lineno, line in enumerate(infile, 1):
wordslist = line.split()
words += len(wordslist)
characters += sum(len(word) for word in wordslist)
print(lineno)
print(words)
print(characters)
This line:
with open('my_words.txt') as infile:
opens the file with the promise to close it as soon as you leave indentation. It is always good practice to close file after your are done using it.
I found this solution very simply and readable:
with open("filename", 'r') as file:
text = file.read().strip().split()
len_chars = sum(len(word) for word in text)
print(len_chars)
with open(filename) as f:
while True:
c = f.read(1)
if not c:
print("End of file")
break
print("Read a character:", c)
First, open a file:
with open("filename") as fileobj:
for line in fileobj:
for ch in line:
print(ch)
This goes through every line in the file and then every character in that line.