-2

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: enter image description here

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:

enter image description here

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

enter image description here

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.

simone100
  • 43
  • 1
  • 6
  • 2
    **nowhere** in the expression do you use pathlib: `('./Spedire/comune_min_corr_'+str(comune)+'.gpkg').replace('./Spedire/comune_min_corr_'+str(comune)+'.gpkg','./Spedire/zp_'+str(comune)+'.gpkg')` where do you think you are using pathlib? – juanpa.arrivillaga Jan 09 '23 at 15:52
  • 1
    BTW, [here is the pathlib documentation](https://docs.python.org/3/library/pathlib.html) – juanpa.arrivillaga Jan 09 '23 at 15:53
  • @nowhere probably I don't use. To use "replace" is not enough. Show me how to use to reproduce the result of os.replace. os.replace() and Path.replace() should be equivalent. But unfortunately I am not able to use Path.replace(). – simone100 Jan 09 '23 at 16:00
  • 2
    you aren't using `pathlib` *anywhere* in your code! you are using the string methods – juanpa.arrivillaga Jan 09 '23 at 16:01
  • @juanpa.arrivillaga I know this link. Before to ask I looked for. But the documentation does not help me. – simone100 Jan 09 '23 at 16:01
  • 1
    you need to create `pathib.Path` objects. You are using `str` objects – juanpa.arrivillaga Jan 09 '23 at 16:03
  • good that you confirm me that I am not using pathlib. Now I need someone that use pathlib to obtain the same result of the os.replace approach. – simone100 Jan 09 '23 at 16:04
  • @juanpa.arrivillaga I have added Path=('Spedire') in my code modifying the path, nothing has changed. "Spedire" is my relative directory – simone100 Jan 09 '23 at 16:10
  • 2
    It's looking like you don't actually know what a module is, or how to import one, or how to instantiate a class. You should start by looking that up. – user2357112 Jan 09 '23 at 16:18
  • The module has been imported with "from pathlib import Path". The edited code has now the object pathlib. If it does not have, show me how I have to create. My relative path is ./Spedire/filename.gpkg – simone100 Jan 09 '23 at 16:23
  • You have to start with `variable = Path('.') / 'Spedire' / (filename + '.gpkg')`. Then you can use `variable.replace(new path)` – Barmar Jan 09 '23 at 16:36
  • @Barmar I assume that by adding Path(‘.’) to “variable”, you can instantiate with replace. However we also could have: variable=Path (‘Spedire’) / (filename+’.gpkg’). Right? – simone100 Jan 09 '23 at 17:13
  • Sure. I was just replicating your `./Spedire` – Barmar Jan 09 '23 at 17:15
  • the name of the variable: `Path=('Spedire')` is irrelevant. you are still not using pathlib or the `Path` type. – juanpa.arrivillaga Jan 09 '23 at 17:20
  • @Barmar I will change the code later. There is still something strange. Why with os.replace it worked even if I did not have a variable called os containing an object of module os? – simone100 Jan 09 '23 at 17:25
  • `os` is a module. https://docs.python.org/3/library/os.html#os.replace – Barmar Jan 09 '23 at 17:31

1 Answers1

0

@Barmar I changed now and it works. Thank you

To resume:

with os.replace() it work without defining a variable because replace() is a function of the module "os".

with "path.replace" we have to do so:

for comune in df4:
variable = Path('Spedire')/f'comune_min_corr_{comune}.gpkg'
variable.replace(Path('Spedire')/f'zp_{comune}.gpkg')

In fact, we have to build an object (here called "variable") because "path.replace" is a class of the module "pathlib". Class always needs to have an object to obtain something because otherwise python does not know what we are talking about.

To be noted: I probably have done something similar, but I repeateadly worked confusing "gpkg" with "gpgk". This trivial mistake leads me in the wrong way.

If I told something wrong, please correct me.

simone100
  • 43
  • 1
  • 6
  • You can read more about the distinctions between classes, instances, objects, and variables in the [official Python tutorial](https://docs.python.org/3/tutorial/classes.html). Some notes: `Path.replace` is a method of the `Path` class, not a class itself. `Path` is one of several classes defined in the `pathlib` module. Also, the last line of your snippet could be written more expressively as `variable.replace(variable.with_name(f'zp_{comune}.gpkg'))` – Brian61354270 Jan 09 '23 at 20:09