python删除图片_一个python批量删除图片的程序,到底哪儿出了问题呢?

用web.py搭了一个站,用户上传一张图片后自动生成3张不同大小的缩略图,用户点击删除图片按钮后,自动把系统目录里的原图和三张缩略图删除。

图片是按日期保存的,整个目录结构如下:

-static

--upload

---post_img

----2012(年)

-----11(月)

------9(日)注:年月日都是程序自动建立的

-------20121194514_5624f3ccbc039b676ece4f74e26ad0bf.jpg(原图)

-------20121194514_5624f3ccbc039b676ece4f74e26ad0bf_1200.jpg(.crop得到的缩略图)

-------20121194514_5624f3ccbc039b676ece4f74e26ad0bf_750.jpg(.crop得到的缩略图)

-------20121194514_5624f3ccbc039b676ece4f74e26ad0bf_365.jpg(.crop得到的缩略图)

我最原先的处理方式是:删除图片时,前端传给后端python一个参数 x : "/static/upload/post_img/2012/11/9/20121194514_5624f3ccbc039b676ece4f74e26ad0bf", 也就是图片文件的一小部分路径, 然后在后台组装,再删除:

#主要代码部分:

homedir = os.getcwd()

imgPath = homedir + x

os.remove(imgPath + '.jpg')

os.remove(imgPath + '_1200.jpg')

os.remove(imgPath + '_750.jpg')

os.remove(imgPath + '_365.jpg')

但是这么做有几个缺点:

1:如果用户上传的不是.jpg文件,而是png或者gif,那么os.remove(imgPath + '.jpg')删除原图时,就会找不到路径,其他的几个1200、750、365是crop()出来的,能保证是.jpg,可以正常删除。 而如果把每个上传上来的原图都自动转成.jpg,感觉有些复杂,而且没有必要。

2:四张图要分别.remove()四次,效率低下,代码不够简洁。

所以我想,能不能向后端传两个参数,一个是图片的保存目录,即 /static/upload/post_img/2012/11/9/,一个是图片的前半部分文件名,也就是 20121194514_5624f3ccbc039b676ece4f74e26ad0bf ,然后根据这个前半部分文件名来一次性批量删除图片:只要文件名包含这个参数的,就全部删除,不管是png还是jpg、gif。

于是我重新写了一段代码:

同时,为了方便测试,我先在网站根目录里建了一个test.py文件,用来模拟网站程序对这个目录的操作。test.py代码和上面的一样,只不过是把 part_name 和 path 这两个网站前端传过来的参数写死了而已。

但是很快我发现有问题,在目录下运行test.py时能正常删除文件(原图和3个缩略图一起批量删除),但是网站程序只能删除一个原图。

这到底是什么一回事?是跟文件的权限有关系吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,可以使用Python的Tkinter库来创建窗口程序,并使用PyPDF2库来处理PDF文件。以下是一个简单的示例程序: ```python import os import tkinter as tk from tkinter import filedialog from tkinter import messagebox import PyPDF2 class PDFtoTXTConverter: def __init__(self, master): self.master = master master.title("PDF to TXT Converter") # Create GUI elements self.label = tk.Label(master, text="Select PDF files to convert:") self.label.pack() self.file_listbox = tk.Listbox(master, height=10) self.file_listbox.pack() self.add_button = tk.Button(master, text="Add Files", command=self.add_files) self.add_button.pack() self.remove_button = tk.Button(master, text="Remove File", command=self.remove_file) self.remove_button.pack() self.convert_button = tk.Button(master, text="Convert", command=self.convert_files) self.convert_button.pack() # Initialize file list self.files = [] def add_files(self): # Open file dialog to select PDF files filetypes = (("PDF files", "*.pdf"), ("All files", "*.*")) new_files = filedialog.askopenfilenames(title="Select PDF files", filetypes=filetypes) # Add selected files to list for file in new_files: if file not in self.files: self.files.append(file) self.file_listbox.insert(tk.END, file) def remove_file(self): # Remove selected file from list selection = self.file_listbox.curselection() if selection: index = selection[0] file = self.files[index] self.file_listbox.delete(index) self.files.remove(file) def convert_files(self): # Convert each PDF file to TXT format for file in self.files: try: # Open PDF file with open(file, 'rb') as pdf_file: pdf_reader = PyPDF2.PdfFileReader(pdf_file) text = "" # Iterate through each page of PDF file for page_num in range(pdf_reader.numPages): page = pdf_reader.getPage(page_num) # Skip pages with images if '/XObject' in page['/Resources']: continue # Extract text from page text += page.extractText() # Save text to TXT file with same name as PDF file txt_file = os.path.splitext(file)[0] + '.txt' with open(txt_file, 'w') as output_file: output_file.write(text) # Show success message messagebox.showinfo("Conversion Complete", "PDF files successfully converted to TXT format.") except Exception as e: # Show error message messagebox.showerror("Conversion Error", str(e)) # Create window root = tk.Tk() app = PDFtoTXTConverter(root) root.mainloop() ``` 该程序创建一个窗口,其中包含三个按钮:Add Files(添加文件)、Remove File(删除文件)和Convert(转换)。用户可以使用Add Files按钮选择要转换的PDF文件,并使用Remove File按钮删除选定的文件。一旦用户选择了要转换的文件,他们可以单击Convert按钮将它们转换为TXT格式。 该程序使用PyPDF2库中的PdfFileReader类来打开每个PDF文件,并使用extractText方法从每个页面中提取文本。程序还检查每个页面是否包含图像,并跳过包含图像的页面,以避免将图像转换为文本。 最后,程序将提取的文本保存到与源PDF文件相同的文件名的TXT文件中。如果现任何错误(例如文件无法打开或保存),程序将显示一个错误消息框。否则,它将显示一个成功消息框。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值