Skip to content

Instantly share code, notes, and snippets.

@Tafhimbn
Last active April 5, 2023 21:50
Show Gist options
  • Save Tafhimbn/2c92cfa824449a1e3a65a13f440b75c9 to your computer and use it in GitHub Desktop.
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
#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()
# 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()
# 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()
# 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()
@shanar88
Copy link

file = open("school_prompt2.txt" , "r")
num_char = 0
for n in file:
num_char += len(n)
print(num_char)

@Yurii1322
Copy link

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