简体   繁体   中英

WindowsError: [Error 3] The system cannot find the path specified

I am doing a beginner course in Python from udacity.

I am following the code but I got an error.

My code is:

import os
def rename_files():

    file_list = os.listdir(r"C:\Users\User\Downloads\prank.zip\prank")


    saved_path = os.getcwd()
    print("current working directory is" +saved_path)
    os.chdir(r"C:\Users\User\Downloads\prank.zip\prank")

    for file_name in file_list:
      os.rename(file_name, file_name.translate(None, "0123456789"))
    os.chdir(saved_path)

rename_files()

It's supposed to get rid of the digits in the file names. However, I get these errors:

WindowsError: [Error 3] The system cannot find the path specified: 'C:\\Users\\User\\Downloads\\prank.zip\\prank/*.*'

You're trying to get a directory listing of a zip file.

While Windows allows you to browse the zip file from Windows Explorer, no programs (including Python) are able to actually see the contents of the file without you first extracting it.

You will need to first extract that zip file to a directory on your local machine and then provide that path instead to your script.

If you want to unzip the file using python, you could do the following:

import zipfile

with zipfile.ZipFile('C:\Users\User\Downloads\prank.zip', 'r') as z:
    z.extractall("C:\Users\User\Downloads\prank")

Then you could pass C:\\Users\\User\\Downloads\\prank to os.listdir in your script.

Thank you for the response!

This is my code now:

import os

import zipfile

with zipfile.ZipFile('C:\\Users\\User\\Downloads\\prank.zip', 'r') as z: z.extractall("C:\\Users\\User\\Downloads\\prank")

def rename_files():

file_list = os.listdir(r"C:\Users\User\Downloads\prank")


saved_path = os.getcwd()
print("current working directory is" +saved_path)
os.chdir(r"C:\Users\User\Downloads\prank")

for file_name in file_list:
  os.rename(file_name, file_name.translate(None, "0123456789"))
os.chdir(saved_path)

rename_files()

I still do not get the result. The shell tab just says:current working directory isC:\\Python27

What am I doing wrong?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM