I wrote a python program to synchronies one of my source files directory. The program takes in user input of the directory to be used. When I run the code from within VS code, the program can access the directories.
I’m on a Mac, so this would be the one of the users input: /Users/dennis/Desktop/Phonenix Enigma Distro
But, when I try to run the program from a terminal, it can’t find the directory. The code for the program is in my document directory: /Users/dennis/Documents/Python/Projects/Python backup software
Here is the code that takes the user input and then verifies the directory;
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 Endingn')
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 Endingn')
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 Endingn')
else:
print(f'Target directory {target} validated.n')
line += f'Target directory {target} validated.n'
else:
Print_Help
sys.exit()
return source, target
I’m new to python, so the program may not be up to a lot of peoples standards. So, what am I missing? Do I need to set the working dorectory to the root first? Am I inputting the directory incorrectly?