Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.6 pathlib Path change name parent directory

The new Path package from the pathlib library, which has been added from Python 3.4, seems a powerful replacement of approaches such as os.path.join(), but I've some trouble working with it.

I have a path that can be anything from folder_foo/file.csv to long/path/to/folder_foo/file.csv. I read the .csv file in folder_foo with pandas, modify it and want to save it to folder_bar/file.csv or long/path/to/folder_bar/file.csv.

Essentially I want to rename folder_foo to folder_bar in the Path object.

EDIT: example path code

csv_path = Path("long/path/to/folder_foo/file.csv")

Attempts

1

csv_path.parents[0] = csv_path.parents[0] + "_clean")

Which leads to the error TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str', which means you cannot use + to combine a PosixPath with a str as described in TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'.

2

To solve this I tried the following:

csv_path.parents[0] = Path(str(csv_path.parents[0]) + "_clean")

Which however results in the error : TypeError: '_PathParents' object does not support item assignment.

Since PosixPath is not a list, this error is understandable.

3

Maybe .parts is a better approach, but

csv_path.parts[-2] = csv_path.parts[-2][:-3] + "bar"

results in: TypeError: 'tuple' object does not support item assignment.

Question

How can I easily rename the file's parent folder?

like image 681
NumesSanguis Avatar asked Jun 21 '18 02:06

NumesSanguis


People also ask

How do I change directory in Python Pathlib?

Python pathlib change directory We go inside another directory with os' chdir . #!/usr/bin/python from pathlib import Path from os import chdir path = Path('..') print(f'Current working directory: {path. cwd()}') chdir(path) print(f'Current working directory: {path. cwd()}') chdir('..')

Is Pathlib path PathLike?

pathlib. Path (WindowsPath, PosixPath, etc.) objects are not considered PathLike : PY-30747.

How does Pathlib path work?

With pathlib , file paths can be represented by proper Path objects instead of plain strings as before. These objects make code dealing with file paths: Easier to read, especially because / is used to join paths together. More powerful, with most necessary methods and properties available directly on the object.

How do I add a directory to a path in Python?

Clicking on the Environment Variables button o​n the bottom right. In the System variables section, selecting the Path variable and clicking on Edit. The next screen will show all the directories that are currently a part of the PATH variable. Clicking on New and entering Python's install directory.


1 Answers

Would rather split this up for readability:

bar_folder = csv_path.parent.parent / 'folder_bar'
csv_path2 = bar_folder / csv_path.name

Having the destination folder as a variable also enables you to create the folder using for example:

bar_folder.mkdir(exist_ok=True)
like image 193
user3066565 Avatar answered Sep 20 '22 12:09

user3066565