简体   繁体   English

python:解压缩错误“没有这样的文件或目录”

[英]python : unzipping error 'no such file or directory'

I have zip file downloaded from website. 我有从网站下载的zip文件。 I wanted to make script that rename zip file and before unzip, it checks how many files are in it and unzip it. 我想制作重命名zip文件的脚本,然后在解压缩之前先检查其中有多少文件并解压缩。

The problem is that zip file is in the directory but it keep giving me error that 'FileNotFoundError: [Errno 2] No such file or directory: 'filename.zip'' I assumed that It might be caused by file name because I use ubuntu and when I downloaded the file, the name was broken because it was not English. 问题是该zip文件位于目录中,但它一直给我以下错误消息:“ FileNotFoundError:[Errno 2]没有此类文件或目录:'filename.zip”,我认为这可能是由文件名引起的,因为我使用的是ubuntu当我下载该文件时,该名称已损坏,因为它不是英语。 so I changed it into numbers (ex:20176) but still getting this error. 所以我将其更改为数字(例如:20176),但仍然收到此错误。

my script path means absolute path. 我的脚本路径表示绝对路径。

    data_type = '{}{}'.format('201706', '.zip')
    filename = [i for i in os.listdir('user/directory')]
    filename.sort(key=lambda ctime: ctime[0])
    downloaded = str(filename[0])

    old = os.path.join('user/directory', downloaded)
    new = os.path.join('user/directory', data_type)
    os.rename(old, new)

    zip = ZipFile(data_type)
    archived_files = zip.namelist()
    amount = len(archived_files)

Let's suppose the first filename in the sorted list is myfile.txt . 假设排序列表中的第一个文件名是myfile.txt Your code 您的密码

old = os.path.join('user/directory', downloaded)
new = os.path.join('user/directory', data_type)
os.rename(old, new)

renames the first file in the directory listing, user/directory/myfile.txt (not, due to the considerations above, the oldest one) to user/directory/201706.zip . 重命名所述第一文件在目录列表中, 用户/目录/ myfile.txt的 (未,由于上述考虑,最旧的一个), 用户/目录/ 201706.zip。 The next statement then tries to open 2010706.zip , which of course doesn't exist. 然后,下一条语句尝试打开2010706.zip ,这当然是不存在的。 It should work if you try 如果您尝试它应该可以工作

zip = ZipFile(new)

Unfortunately there's no guarantee that the file actually will be a zipfile, so the operation may fail. 不幸的是,不能保证该文件实际上将是一个zipfile,因此该操作可能会失败。

Some other points to consider, perhaps in other questions: 可能需要考虑其他一些问题:

I suspect you are misunderstanding the sorting functions: although you appear to want to sort the list of filenames on creation time, just calling a lambda's parameter ctime doesn't mean that Python will understand your needs. 我怀疑您对排序功能有误解:尽管您似乎想在创建时对文件名列表进行排序,但仅调用lambda的参数ctime并不意味着Python会理解您的需求。 If only programming languages had a DWIM ("do what I mean") mode, life would be so much easier! 如果仅编程语言具有DWIM(“按我的意思做”)模式,那么生活将会更加轻松!

The key argument to sort is a function that the sort function calls once for each value to be sorted, expecting it to return a "sort key" (that is, a value that represents its place in the required ordering). sortkey参数是sort函数针对要排序的每个值调用一次的函数,期望它返回“ sort key”(即代表其在所需顺序中的位置的值)。 Suppose I take your lambda and apply it to a filename: 假设我将您的lambda应用于文件名:

In [1]: ruth_lambda = lambda ctime: ctime[0]

In [2]: ruth_lambda("MY_FILENAME.TXT")
Out[2]: 'M'

You can see that you are sorting on the first character of the filename, and I doubt that's what you really want. 您会看到您正在对文件名的第一个字符进行排序,而我怀疑那是您真正想要的。 But we can go into that later. 但是我们可以稍后再讨论。

A quick note on formatting: it would have been simpler to write 关于格式化的快速说明:编写起来会更简单

data_type = '{}{}'.format(zipfile_name, '.zip')

as

data_type = '{}.zip'.format(zipfile_name)

Finally, since os.listdir returns a list, instead of 最后,由于os.listdir返回一个列表,而不是

filename = [i for i in os.listdir('user/directory')]

it's simpler to write 写起来更简单

filename = os.listdir('user/directory')

though the extra computation will do no harm. 尽管额外的计算不会造成任何损害。 As a beginner you will find that as you improve, your older code starts to look really clunky - don't worry about that, it's a common experience! 作为一个初学者,您会发现随着您的改进,您的旧代码开始显得笨拙-不用担心,这是一种常见的体验! Just move forwards and try not to repeat old mistakes. 只要向前迈进,并尽量不要重蹈覆辙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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