2
I am working with hundreds of pdf files, I have to rename them with the folder name where they are allocated.
for example:
pasta
|--- pastajoão
|-- celular.pdf
|--- pastamaria
|-- caderno.pdf
has how to use python to edit the name of the files leaving this way?:
pasta
|--- pastajoão
|-- pastajoão_celular.pdf
|--- pastamaria
|-- pastamaria_caderno.pdf
I have this code here:
import pathlib import Path
import os
import shutil
path = Path('meu caminho')
for dirss in os.listdir(path=path):
print(dirss)
sleep(1)
for dirs, sub, files in os.walk(path):
for file_ in files:
path = os.path.join(dirs, file_)
shutil.move(path, path)
try:
for _, in path.glob('*.pdf'):
edit = os.listdir()
_.rename(edit)
except Exception:
print('Nenhum arquivo PDF')
he returns me this error
for _, in path.glob('*.pdf'):
TypeError: cannot unpack non-iterable PosixPath object
also if I use the function rename()
he was going to rename the entire file, right?
someone could help me ? :)
Can you use Python to edit the name of the files by leaving it like this?: Yes, but there are errors in your code and I may be wrong, but I don’t think you tried to solve anything, but you just copied and pasted and you want someone to solve your problem, if you edit and show interest in something showing you want to learn I’ll be back to pass a solution
– Guilherme França de Oliveira
That’s just a piece of my code
– user172268
I just need to edit the name of the files.
– user172268
I did a lot of research and found nothing that could help me and my soutions went wrong too.
– user172268
is that the above code does not demonstrate that it has any kind of knowledge about the language
– Guilherme França de Oliveira
@Guilhermefrançadeoliveira I edited my post
– user172268
trying to help you posted below something that can give you the expected result, take a look and if solve your problem just mark the answers that are useful
– Guilherme França de Oliveira
The specific error is happening because you put a
,
infor _, in path.glob('*.pdf'):
- alias, by convention, e m Python, usams_
with a variable name only if it is a "trash basket" - that is, a value that is provided by side effect in some function call and will not be used. In this case it is your path object that you want to rename - better give a real name to variable.– jsbueno