Skip to content

Instantly share code, notes, and snippets.

  • Save T-Bharath/eae33c117baae1d9eb3a8bdb10d60288 to your computer and use it in GitHub Desktop.
Save T-Bharath/eae33c117baae1d9eb3a8bdb10d60288 to your computer and use it in GitHub Desktop.
@WestonSpiro
Copy link

fname = input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"

fh = open(fname)
count = 0
for line in fh:
x=line.rstrip().split()
if 'From' in x:
print(x[1])
count+=1
else:
continue

print("There were", count, "lines in the file with From as the first word")

@KamdiaSJ
Copy link

fname = input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"

fh = open(fname)
count = 0
for line in fh:
x=line.rstrip().split()
if 'From' in x:
print(x[1])
count+=1
else:
continue

print("There were", count, "lines in the file with From as the first word")

@quandovedote
Copy link

Hey, can you explain this line: if len(fname) < 1 : fname = "mbox-short.txt"

@Layla-Aphrodite
Copy link

fname = input("Enter file name: ")
fh = open(fname)
count=0
for line in fh:
line1=line.rstrip()
wds=line1.split()
if len(wds)==0 or wds[0]!='From':
continue
print(wds[1].rstrip())
count=count+1
print("There were",count,"lines in the file with From as the first word")

@Inspire2023
Copy link

Inspire2023 commented Mar 14, 2024

fname = input("Enter file name: ")

if len(fname) < 1:
    fname = "mbox-short.txt"
    
fh = open(fname, "r")
count = 0

for line in fh: 
    if line.startswith("From:"):
        words = line.split()
        #print(words)
        print(words[1])
        count += 1 

print("There were", count, "lines in the file with From as the first word")

@Gingeryong
Copy link

I kept printing out the right lines but every line repeat twice until I realized what the hint is about and change ("From") to ("From ") (add one space at the end) to avoit lines that start with 'From:' !

@Snqa1998
Copy link

Hi.I've written the below code but in server it's showing the error "Error storing grade on server" though the output is exact same as the desired output .Can anyone help what does this error means and what I've to modify in the config?
fname = input("Enter file name: ")
fh = open(fname)
count = 0
for line in fh:
if line.startswith("From "):
address = line.split()
print(address[1])
count +=1
else:
continue

print("There were", count, "lines in the file with From as the first word")

@JohnSha0
Copy link

Hey, can you explain this line: if len(fname) < 1 : fname = "mbox-short.txt"

it causes the file name to be substituted as "mbox-short.txt" if the length of the given text os less that one. i.e., if nothing is typed i.e., if you pressed enter.
if you press enter, filename will be automatically set. no need to type it in

@JohnSha0
Copy link

fname = input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
file = open(fname)
count = 0
for line in file:
if line.startswith("From"):
piece = line.strip().split()
print(piece[1])

print("There were", count, "lines in the file with From as the first word")

I typed this code but it repeats the the mail id if there's multiple. how can i prevent that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment