Mac环境下Python目录同步程序终端运行无法访问目录问题排查
问题排查:Mac终端运行Python目录同步程序无法找到指定目录
我编写了一个用于同步源文件目录的Python程序,该程序支持接收命令行参数或用户输入的目录路径。在VS Code中运行时,程序可正常访问目录(示例输入:/Users/dennis/Desktop/Phonenix Enigma Distro),但在Mac终端运行时无法找到指定目录。程序代码位于/Users/dennis/Documents/Python/Projects/Python backup software路径下,以下是获取并验证目录参数的代码片段。作为Python新手,我想排查问题原因:是否需要先设置工作目录为根目录?还是我输入目录的方式有误?
def Get_Run_Parms(): # The program accepts run time parameters or user input. global line def Print_Help(): # Print put help if need be os.system('Clear') print(f"SyncSource version {ver} Program help.\n") print("-s=<source directory to search>.") print(f"-t=<target directory to copy file to.\n") print("Example:") print(" SourceUpdate.py -s='source/directory' -t='target/directory'") print() print(" SourceUpdate.py -h prints this help menu.") print() print(f'If no run time parms are present, the program will ask for user input.\n') # Check for run time parms args = sys.argv[1:] print(f'\nNumber of parms entered: {len(args)} Parms: {args}\n') line += f'\nNumber of parms entered: {len(args)} Parms: {args}\n' # If 0 run time parms, ask the user for input if len(args) == 1: Print_Help() sys.exit() if len(args) != 2: print('No run time parms entered, please supply the following;') line += f'No run time parms entered, please supply the following;\n' # Ask for source, if no source, bow out. source = input(f'\nEnter source directory: ') line += f'Enter source directory: User Entered: {source}\n' if not os.path.exists(source): print(f'Source directory {source} not found.\n') line += f'Source directory {source} not found.\n' line += f'Program ended.\n' sys.exit(f'Program Ending.\n') else: print(f'Source directory {source} validated.\n') line += f'Source directory {source} validated.\n' # Ask for target, if no target, ask to create or bow out. target = input(f'Enter target directory: ') line += f'Enter target directory: User Entered: {target}\n' if not os.path.exists(target): print(f'Target directory {target} not found.\n') line += f'Target directory {target} not found.\n' a = input('Would you like to create this directory? ') line += f'Would you like to create this directory? User Entered: {a}\n' if a in ['Yes','yes','Y','y']: print(f"Creating directory {target}\n") line += f"Creating directory {target}\n" os.makedirs(target) else: line += f'Program ended.\n' sys.exit(f'Program Ending\n') else: print(f'Target directory {target} validated.\n') line += f'Target directory {target} validated.\n' else: for i in args: # Check for source, no source, bow out line += f"{i[:3]} {i[:3]}\n" if i[:3] == '-s=' or i[:3] == '-S=': source = str(i[3:]) if not os.path.exists(source): print(f'Source directory {source} not found.\n') line += f'Source directory {source} not found.\n' line += f'Program ending.\n' sys.exit(f'Program Ending\n') else: print(f'Source directory {source} validated.\n') line += f'Source directory {source} validated.\n' # Check for target, no target as to create or bow out. elif i[:3] == '-t=' or i[:3] == '-T=': target = str(i[3:]) if not os.path.exists(target): print(f'Target directory {target} not found.\n') line += f'Target directory {target} not found.\n' a = input('Would you like to create this directory? ') line += f'Would you like to create this directory? {a}\n' if a in ['Yes','yes','Y','y']: print(f"Creating directory {target}\n") line += f"Creating directory {target}\n" os.makedirs(target) else: line += f'Program ending.\n' sys.exit(f'Program Ending\n') else: print(f'Target directory {target} validated.\n') line += f'Target directory {target} validated.\n' else: Print_Help sys.exit() return source, target
内容的提问来源于stack exchange,提问作者BigD
相关产品推荐
相关产品推荐

