【发布时间】: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!')
【问题讨论】:
-
你能发布完整的错误吗?