How do I resolve a problem of renaming ENCODED URL files?

Asked

Viewed 42 times

0

How to troubleshoot this problem of renaming ENCODED URL files?

When converting a list of URL ENCODED files through the following code

from urllib.parse import unquote
import os, shutil

cwd = os.getcwd()
print('\nCaminho atual:')
print(cwd)

files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.pdf')]
print('\nListagem dos arquivos PDF encontrados:')
for arq in files:
    print('\t'+unquote(arq))

for arq in files:
    pathname1 = os.path.join(cwd,arq)
    pathname2 = os.path.join(cwd,unquote(arq))
    shutil.move(pathname1,pathname2)

I have a problem. All named files containing ":" (colon -- "%3A"), such as:

Security%20without%20Obscurity%3A%20A%20Guide.pdf
Beginning%20Python%20Visualization%3A%20Crafting%20Visual.pdf

are truncated simply to:

Security without Obscurity
Beginning Python Visualization

And overwritten with ZERO bytes on the original file. As the idea is to change the ENCODED URL name to DECODED URL, this problem I tried to work around with these additional lines

print('\nListagem dos arquivos PDF renomeados:')
for arq in files:
    arq.replace("%3A","-")
    print('\t'+unquote(arq))

But I did not succeed. The replace function seems not to work.

Obs.: I know that this limitation is not python problem, but the Operating System, even so, the problem persists.

  • what is the operating system?

  • Windows. However I believe I found where this error. Only the value was changed, but not passed to display by "print" (in the extra code of the above example). I don’t know if, besides ":", there will be another situation that may occur similar problems.

1 answer

0

The extra code should be changed to

print('\nListagem dos arquivos PDF renomeados:')
for arq in files:
    ajustado = arq.replace("%3A","-")
    print('\t'+unquote(ajustado))

Consequently the original code needs to be changed to

for arq in files:
    pathname1 = os.path.join(cwd,arq)
    pathname2 = os.path.join(cwd,unquote(ajustado))
    shutil.move(pathname1,pathname2)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.