user13364040
user13364040

Reputation:

Trying to use / on Path object Python

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)

Upvotes: 9

Views: 7381

Answers (2)

shraddha mishra
shraddha mishra

Reputation: 11

Considering file and folders path as string solved this error

shutil.move(str(source_path),str(destination_path))

Upvotes: 1

Dunes
Dunes

Reputation: 40833

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()))

Upvotes: 16

Related Questions