Python 名称错误问题
因此,我对Python编码和一般编程都是相当陌生的,我正在尝试编写一个程序来备份给定文件夹中的文件。但是,我继续得到一个“NameError:名称'src'未定义。我看到了一些关于此错误的类似问题,但没有人能让我理解我做错了什么或为什么会出现此错误。据我所知,我在下面的代码中定义'src'。任何帮助都将不胜感激 错误: 文件“/home/student/PycharmProjects/Lab1.py/Lab5.5.py”,第1行,正在处理中 备份(src、dest) NameError:未定义名称“src”Python 名称错误问题,python,backup,nameerror,Python,Backup,Nameerror,因此,我对Python编码和一般编程都是相当陌生的,我正在尝试编写一个程序来备份给定文件夹中的文件。但是,我继续得到一个“NameError:名称'src'未定义。我看到了一些关于此错误的类似问题,但没有人能让我理解我做错了什么或为什么会出现此错误。据我所知,我在下面的代码中定义'src'。任何帮助都将不胜感激 错误: 文件“/home/student/PycharmProjects/Lab1.py/Lab5.5.py”,第1行,正在处理中 备份(src、dest) NameError:未定义名
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!')
尽管您已将其作为命令参数传递,但为什么要提示用户输入
src
和dest
路径?问题可能是因为您在运行脚本时没有提供src
参数。例如
python script.py srcpath-dstpath
您能发布完整的错误吗?