python os.rename(…) won't work !

前端 未结 6 827
离开以前
离开以前 2020-12-22 00:26

I am writing a Python function to change the extension of a list of files into another extension, like txt into rar, that\'s just an idle example. But I\'m getting an error

6条回答
  •  猫巷女王i
    2020-12-22 01:06

    os.rename really doesn't like variables. Use shutil. Example taken from How to copy and move files with Shutil.

    import shutil
    import os
    source = os.listdir("/tmp/")
    destination = "/tmp/newfolder/"
    for files in source:
        if files.endswith(".txt"):
            shutil.move(files,destination)
    

    In your case:

    import shutil
    shutil.move(file_list[entry_pos], new_file_name)
    

提交回复
热议问题