【问题标题】:Issue with NameErrorNameError 问题
【发布时间】:2021-02-20 22:44:32
【问题描述】:

因此,我对 Python 编码相当陌生,总的来说,我正在尝试编写一个程序来备份给定文件夹中的文件。但是,我继续收到“NameError: name 'src' is not defined。我看到了一些与此错误类似的其他问题,但还没有一个问题让我明白我做错了什么或为什么我得到这个错误。至于我明白我在下面的代码中定义“src”。任何帮助将不胜感激。

错误: 文件“/home/student/PycharmProjects/Lab1.py/Lab5.5.py”,第 1 行,正在处理中

备份(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!')

【问题讨论】:

  • 你能发布完整的错误吗?

标签: python backup nameerror


【解决方案1】:

为什么你提示用户输入srcdest 路径,尽管你已经将它们作为命令参数传递了?问题可能来自您在运行脚本时没有提供 src arg 的事实。像

python script.py srcpath dstpath

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 2017-03-14
    • 2021-10-10
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多