FileNotFoundError: [WinError 2] The system cannot find the file specified:

后端 未结 3 369
北荒
北荒 2021-01-02 07:52
import os

def rename(directory):
    for name in os.listdir(directory):
        print(name)
        os.rename(name,\"0\"+name)


path = input(\"Enter the file path\         


        
相关标签:
3条回答
  • 2021-01-02 08:01

    Agreeing with Bernie's answer that "filename" is used to mean the full/absolute path name . The below will also work.

            os.rename((directory+name),(directory+'0'+name))
    
    0 讨论(0)
  • 2021-01-02 08:06

    As written you're looking for a file named 0.jpg in the working directory. You want to be looking in the directory you pass in.

    So instead do:

            os.rename(os.path.join(directory,name), 
                      os.path.join(directory,'0'+name))
    
    0 讨论(0)
  • 2021-01-02 08:06

    You cannot use absolute path unless your terminal is in that directory. Hence you can do as following:

    def rename(directory):
        os.chdir(directory) # Changing to the directory you specified.
        for name in os.listdir(directory):
            print(name)
            os.rename(name,"0"+name)
    
    0 讨论(0)
提交回复
热议问题