Last active
April 5, 2023 21:50
-
-
Save Tafhimbn/2c92cfa824449a1e3a65a13f440b75c9 to your computer and use it in GitHub Desktop.
Using the file school_prompt2.txt, find the number of characters in the file and assign that value to the variable num_char
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Using the file school_prompt2.txt, find the number of characters in the file and assign that value to the variable num_char | |
file=open("school_prompt2.txt","r") | |
st=file.read() | |
num_char = 0 | |
for ch in st: | |
num_char+=len(ch) | |
file.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Find the number of lines in the file, travel_plans2.txt, and assign it to the variable num_lines. | |
file=open("travel_plans2.txt","r") | |
st=file.read() | |
print(st) | |
lst=st.split("\n") | |
num_lines=len(lst)-1 | |
file.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a string called first_forty that is comprised of the first 40 characters of emotion_words2.txt. | |
file=open("emotion_words2.txt","r") | |
first_forty=file.read(40) | |
file.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write code to find out how many lines are in the file emotion_words.txt as shown above. Save this value to the variable num_lines. Do not use the len method. | |
file=open("emotion_words.txt","r") | |
st=file.read() | |
print(st) | |
lst=st.split("\n") | |
num_lines=0 | |
for lin in lst: | |
if lin > lst[-1]: | |
num_lines+=1 | |
file.close() |
file_open_read_lines.py
with open("file.txt", "r") as myfile:
total = 0
for line in myfile.readlines():
print(line, end="")
total += 1
print("Total: " + str(total))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
file = open("school_prompt2.txt" , "r")
num_char = 0
for n in file:
num_char += len(n)
print(num_char)