The program takes an optional command line argument (which is meant to be a directory path)
I am using python pathlib and shutil to move files. Here's the code:
from pathlib import Path
path = Path(sys.argv[1])
shutil.move(path / file, path / e.upper())
Where e is just a string representing certain file extension;
Input:
python3 app.py /home/user/Desktop
This code generates an error:
'PosixPath' object has no attribute 'rstrip'
The / operator works fine if I don't specify the second argument in the command line (and use Path.cwd() as the path instead)
2 Answers 2
Use the rename function of Path to move a file, if you're using the pathlib module.
ie.
(path / file).rename(path / e.upper())
Otherwise, if you wish to use the shutil module, then you must convert your paths to strings before passing them to shutil.move()
ie.
shutil.move(str(path / file), str(path / e.upper()))
Comments
Considering file and folders path as string solved this error
shutil.move(str(source_path),str(destination_path))
NameErrors, becauseshutil,fileandeare not defined. Please post a minimal reproducible example.shutil.moveis not documented to take path-like arguments. Also, please post the entire traceback, not just the error message.