[Python] Python os.rename gives FileNotFoundError because of questionmark in dirname?

Discussão em 'Python' iniciado por Stack, Novembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I'm working on a repo, where someone has named all directories with a "?" in the middle (home/runner/work/repo/repo/bla_123?-1_bla/file). This created all kinds of problems so I wrote a quick github action script that starts the following python script:

    import os
    import re
    def changefilename():
    dir_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..",".."))
    for root, dirs, files in os.walk(dir_path):
    for file in files:
    root_split = root.split("/")
    root_split[-1] = re.sub(r"([^\?]*)\?(.*)", r"\1\2", root_split[-1])
    new_root = "/".join(root_split)
    print(file, root, new_root) #for debugging
    os.rename(os.path.join(root, file),os.path.join(new_root, file))

    changefilename()


    I know that the yml file is not the problem (I did something extremly similar but on a different repo and just copied from there). So the problem must be in the script or on the filenames. I get a FileNotFoundError on os.rename for a file adress where I know that it exists. (The print() command alone also works great and prints out the corrected root for all files in the repo, if os.rename() is removed.)

    FileNotFoundError: [Errno 2] No such file or directory: 'home/runner/work/repo/repo/bla_123?-1_bla/file' -> 'home/runner/work/repo/repo/bla_123-1_bla/file'


    My question therefor is whether the "?" might cause these issues and what i could do to work around that.

    (PS: For context: The yml file contains runs-on: ubuntu-latest, which is why I used "/".join())

    Continue reading...

Compartilhe esta Página