Maxwell's Daemon
Maxwell's Daemon

Reputation: 631

rename files with pathlib

I'm trying to rename files in a folder using the name of other files that are on a different folder but I'm getting some errors.

The files I'm trying to rename are in the 'OUT' folder. The filenames I'm trying to use are the ones in the 'IN' folder.

This is a sample of the files in my current directory:

tree
├── IN
│   ├── Lost (Perdidos) 1x01 - Piloto (Parte 1).mkv
│   ├── Lost (Perdidos) 1x02 - Piloto (Parte2).mkv
│   └── Lost (Perdidos) 1x03 - Tabla Rasa.mkv
├── OUT
│   ├── 1x01 - Lost [x265].mkv
│   ├── 1x02 - Lost [x265].mkv
│   └── 1x03 - Lost [x265].mkv
├── rename.py

The code I'm using:

import glob
import os
from pathlib import Path


IN_DIR=Path("IN")
OUT_DIR=Path("OUT")

current_DIR = Path.cwd()
print(f"Current folder: {current_DIR}")
new_DIR = current_DIR / IN_DIR

for f1, f2 in zip(
    sorted(IN_DIR.glob("*.mkv")),
    sorted(OUT_DIR.glob("*.mkv"))): 
    os.chdir(new_DIR)
    print(f"Entering in: {Path.cwd()}")
    if f2.name[:4] in f1.name:
        print(f"{f2.name} will change to {f1.name[16:]}\n")
        old = Path(f2.name)
        print(old.is_file())
        new = Path(f1.name[16:])
        old.rename(new)
        print(f"Going back to : {os.chdir(current_DIR)}")

This is the output of the script:

Current folder: /datos/Series/Incompletas/Lost
Entering in: /datos/Series/Incompletas/Lost/IN
1x01 - Lost [x265].mkv will change to 1x01 - Piloto (Parte 1).mkv

Old name: 1x01 - Lost [x265].mkv
False
New name: 1x01 - Piloto (Parte 1).mkv
Traceback (most recent call last):
  File "rename.py", line 28, in <module>
    old.rename(new)
  File "/usr/lib/python3.8/pathlib.py", line 1361, in rename
    self._accessor.rename(self, target)
FileNotFoundError: [Errno 2] No such file or directory: '1x01 - Lost [x265].mkv' -> '1x01 - Piloto (Parte 1).mkv'

I'm really puzzled because I'm accessing the folder where the files are, but it looks like the files are not there.

[UPDATE] It looks like the files I'm trying to access are not files. Therefore, I'm getting an error.

Upvotes: 1

Views: 3828

Answers (1)

CryptoFool
CryptoFool

Reputation: 23079

Your problem is that you are specifying only the filenames to the os.rename() function, but the current directory is not the OUT directory containing those files. The best thing to do is to add the output directory to the two paths involved in the rename so that you can make the call from the directory you're in:

import glob
import os
from pathlib import Path


IN_DIR=Path("IN")
OUT_DIR=Path("OUT")

current_DIR = Path.cwd()
print(f"Current folder: {current_DIR}")
new_DIR = current_DIR / IN_DIR

for f1, f2 in zip(
    sorted(IN_DIR.glob("*.mkv")),
    sorted(OUT_DIR.glob("*.mkv"))): 
    if f2.name[:4] in f1.name:
        old = OUT_DIR.joinpath(f2.name)
        new = OUT_DIR.joinpath(f1.name[16:])
        print("{} ===> {}".format(old, new))
        old.rename(new)

File names in OUT after run:

1x01 - Piloto (Parte 1).mkv
1x02 - Piloto (Parte 2).mkv
1x03 - Tabla Rasa.mkv

Upvotes: 2

Related Questions