Charles
Charles

Reputation: 17

Issue with NameError

So I am fairly new with coding in Python and in general and I am trying to write a program that will backup files in a giving folder. However, I continue to get a "NameError: name 'src' is not defined. I see some other questions similar about this error but none have yet to make me understand what I am doing wrong or why I get this error. As far as I understand it I am defining 'src' in the code below. Any help would be greatly appreciated.

ERROR: File "/home/student/PycharmProjects/Lab1.py/Lab5.5.py", line 1, in processing

backup(src, dest)

NameError: name 'src' is not defined

def backup(src, dest):
#Checking if src and dest directories exist
    sourceFilePath = input('Enter folder path to be backed up')
    destFilePath = input('Please choose where you want to place the backup')
    #found = true
    for directory in [src, dest]:
        if not isdir(directory):
            print(f'could not find {directory}')
        found = False
    if not found:
        exit(1)
#for each file in src
    for sourceFileName in listdir(src):
    #computing file paths
        sourceFilePath = path.join(src, sourceFileName)
        destFilePath = path.join(dest, sourceFileName)
    #backing up file
    copy2(sourceFilePath, destFilePath)
#entry point
    if __name__=='__main__':

    #validating length of command line arguments
        if len(argv) != 3:
            print(f'Usage: {argv[0]} SRC DEST')
        exit(1)

    #performing backup
    backup(argv[1], argv[2])

    #logging status message
    print('Backup succesful!')

Upvotes: 0

Views: 713

Answers (1)

Mahery Ranaivoson
Mahery Ranaivoson

Reputation: 450

why are you prompting the user for src and dest path though you already pass them as command args? The issue probably came from the fact you didn't provide the src arg while running the script. Things like

python script.py srcpath dstpath

Upvotes: 1

Related Questions