0

I have 1000+ files in 2 folders(same amount).

EXAMPLE

Folder1/
   0.json
   1.json
   2.json

Folder2/
   some_name1.xml
   some_name2.xml
   some_name3.xml

I want to rename all files in 1st folder based on second folder file names BUT NOT THE EXTENSIONS

I got this script. It must just rename all files one by one But now it got a problem , after renaming files there are shuffled which means information are not same as files in folder1. Any ideas how to fix that?

from pathlib import Path

src = Path(r"/home/folder2")
dst = Path(r"/home/folder1")
for s, d in zip(src.iterdir(), dst.iterdir()):
    d.rename(d.with_name(s.with_suffix(d.suffix).name))
alexuuu
  • 53
  • 5
  • It is really not clear... Can you give a [mre] (like 3 files) of input and expected output? – Tomerikoo Nov 26 '20 at 17:14
  • @Tomerikoo Update with all information – alexuuu Nov 26 '20 at 17:18
  • And can specify what is the problem? What is your expected output and what is your current? – Tomerikoo Nov 26 '20 at 17:24
  • Your code is renaming all files in Folder2 (the `dst` folder), no? – Booboo Nov 26 '20 at 17:26
  • From the [docs](https://docs.python.org/3/library/pathlib.html#pathlib.Path.iterdir): *The children are yielded in __arbitrary order__*. Is that your problem? – Tomerikoo Nov 26 '20 at 17:27
  • @Booboo sorry renaming Folder1 , I update – alexuuu Nov 26 '20 at 17:35
  • @Tomerikoo yep exactly. – alexuuu Nov 26 '20 at 17:39
  • So what is your question? Again, please post your expected output and the one you get and formulate a clear question – Tomerikoo Nov 26 '20 at 17:43
  • Expected output , is just rename all files names(not extensions) in folder1 based on folder2 file names with same order. It means if first file got some info it must be 1st after renaming. Right now it shuffled. – alexuuu Nov 26 '20 at 17:48
  • Instead of using `iterdir` you will have to make a list of files and sort them – Tomerikoo Nov 26 '20 at 17:50
  • Does this answer your question? [Is there a built in function for string natural sort?](https://stackoverflow.com/questions/4836710/is-there-a-built-in-function-for-string-natural-sort) – Tomerikoo Nov 26 '20 at 17:50
  • 2
    You can try: `for s, d in zip(sorted(src.iterdir()), sorted(dst.iterdir())):` But: `9.json` will come after `10.json` but then again `some_name9.xml` will come after `some_name10.xml`, so maybe that is OK. Otherwise, you will have to write your own `key` function. – Booboo Nov 26 '20 at 17:54
  • @Booboo ty you u helped me! – alexuuu Nov 26 '20 at 18:09

1 Answers1

0

The following is an example of a key function that will use the first integer found within a file name as the sort key, otherwise just the file name (all files that do not contain integers within their file names will come last in the sort):

from pathlib import Path
import re
import math

rex = re.compile(r'\d+')
def my_key_function(path):
    m = rex.search(path.name)
    # file names without integers in them will be sorted last:
     return (int(m[0]) if m else math.inf, path.name)

for s, d in zip(sorted(src.iterdir(), key=my_key_function()), sorted(dst.iterdir(), key=my_key_function)):
    etc.
Booboo
  • 38,656
  • 3
  • 37
  • 60