TransWikia.com

I need with perfecting a project for beginners in terms of writing to a file

Stack Overflow Asked by GetEmOuttaHere on December 9, 2021

I’m trying to make sure the input the user uses is all letters.I tried the .alpha method but since this is a file, a "." will be included returning it false. I also tried using "quit" sentinel value to exit the program but that isn’t working. It keeps saying break is outside the loop. I also want the user to keep inputting if the file is not found error is raised.
The Assignment1

def main():
 fileName = inputTxt()
 FiletoReadFrom = openingFile(fileName)
 counter = 0
 for line in FiletoReadFrom:
        outputFile = open("output.txt", "a+")
        counter = counter + 1
        outputFile.write("/* {} */ {}n".format(counter, line.strip()))
 if counter == 0:
     print("This is an empty file")
 else:
     print("The file has been made")
     FiletoReadFrom.close()
     outputFile.close()
 
 
def inputTxt():
    flag = True
    while flag == True:
        FileName= input("Please Enter the Name of the File, or type quit to exit ")
        if FileName == quit:
            flag == False
            break
            print("goodbye")
        else:
            return FileName
    
     
def openingFile(filetoReadFrom):
 try:
    a = open(filetoReadFrom, 'r')
    return a
 except FileNotFoundError:
       print("This File was not Found", "Enter again or type quit to exit")
 

main()

One Answer

There are different questions here, which is frowned upon on this site. Please never do that again.

  1. the quit and break problem:

    It is just a typo. As you forgot quoting 'quit', Python sees it at an undeclared variable which gives a syntax error. Fix:

     ...
     while flag == True:
         FileName= input("Please Enter the Name of the File, or type quit to exit ")
         if FileName == 'quit':
             flag == False
             break
             ...
    

    But it is still wrong, because break will only exit from the loop and inputTxt will return None which is not what you expect. Calling sys.exit() could be better here.

  2. Test for letters and not numbers:

    You must choose a white list (only alphas and dot) or black list (no numbers) way. In idiomatic Python it could be:

     if all((x.isalpha() or x == '.') for x in FileName):  # white list
         # process error condition
    
     if any(x.isdigit() for x in FileName):                # black list
         # process error condition
    

    You could also use the re module which is great at controlling that a string respect a given pattern...

  3. keep asking until a valid file is given:

    You should use a loop:

     def main():
         while True:
             fileName = inputTxt()
             FiletoReadFrom = openingFile(fileName)
             if FileToReadFrom is not None:  # openingFile returns None when file does not exist
                 break
    

    But IMHO, you should remove the openingFile function and directly use (and test) open in main

Answered by Serge Ballesta on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP