Pathlib is suggested instead of os.module because is newer. However I find less documentation about Pathlib if I look for. I show an issue that give me problems.
Initial condition
In a directory I have the following files:
I want to change the prefix of the files "comune_min_corr_" in "zp".
OS approach
With the following code where df4 is dataframe that contain "comune":
for comune in df4:
os.replace('./Spedire/comune_min_corr_'+str(comune)+'.gpkg', './Spedire/zp_'+str(comune)+'.gpkg')
To be clear, "comune" in the following image are three items: Airolo, Alto Malcantone, Aranno. They are part of the filename.
The result is right, the files "zp_comune.gpkg" are replaced by the files "comune_min_corr_comune.gpkg" and "comune_min_corr_comune.gpkg" disappear:
Pathlib approach
I create again the "initial condition".
Then, I try to do the same thing than before, adapting for the "pathlib.replace(target)". They say here https://realpython.com/python-pathlib/ that we have to use "source.replace(destination)" structure. I did with the following code.
for comune in df4:
Path=('Spedire')
('/comune_min_corr_'+str(comune)+'.gpkg').replace('/comune_min_corr_'+str(comune)+'.gpkg','/zp_'+str(comune)+'.gpkg')
It give me no error but it does not work, the content of the directory does not change
Question Could you provide the solution to obtain the same result for pathlib that I obtain with os? Please explain your step and why it does not work for pathlib.
Thank you.