Windows Error: 32 when trying to rename file in python

2024/10/15 15:22:20

I'm trying to rename some PDF files using pyPdf and my code it seems to work fine until it reaches the rename sentence. The While/if block of code looks for the page number where string "This string" is located and when found stops. Having the page number the "new name" is created.

My issue is that even when the with block it's supposed to close automatically the file, when it's reached the rename sentence I get the error below

Traceback (most recent call last):
File "<stdin>", line 14, in <module>
WindowsError: [Error 32] The process cannot access the file because it is being used by another process

and I don't know how to close the file before rename it, since if I use "file.close()" I get this error

Traceback (most recent call last):
File "<stdin>", line 14, in <module>
AttributeError: 'str' object has no attribute 'close'

My current code is below, thanks for any help on this.

import os
import glob
import sys
from os.path import basename
import pyPdfpath = "C:\\My\\Path\\"
os.chdir(path)  
for file in glob.glob("*.pdf"):print filei = 0with open(file, "rb") as f:pdf = pyPdf.PdfFileReader(f)while True:txt = pdf.pages[i].extractText()if "This string" in txt:new_name = basename(file) + "_Page_" + str(i)breaki = i + 1   print new_name#file.close()os.rename(file, new_name) # The error occurs here.

* Update *

Without With block I get the same error

for file in glob.glob("*pdf"):print filei = 0   f = open(file, "rb") pdf = pyPdf.PdfFileReader(f)while True:txt = pdf.pages[i].extractText()if "This string" in txt:new_name = basename(file) + "_Page_" + str(i)break           i = i + 1   f.close()   os.rename(file, new_name)
Answer

Thanks your advice .But,I remove the variable in my program,and successed. :) In my view ,the file was surely opened in my program ,but I dont know how to c lose it,So I clean All. :(

https://en.xdnf.cn/q/117815.html

Related Q&A

I dont quite understand the while loop in python

def AddSingleCard(self):symbols = [heart, diamond, club, spade]#newCardSign = newCardNumber, newCardSign = raw_input().split()try:newCardNumber = int(float(newCardNumber))except:newCardNumber, newCardS…

Adding a FileField to a custom SignupForm with django-allauth

I have the following custom SignupForm (simplified, works perfectly without my_file):class SignupForm(forms.Form):home_phone = forms.CharField(validators=[phone_regex], max_length=15)my_file = forms.Fi…

Checkbox to determine if an action is completed or not

I have a list of dictionaries of clients in a format like this:dict_list = [{Name of Business : Amazon, Contact Name : Jeff Bezos, Email : [email protected]}, {Name of Business : Microsoft, Contact Nam…

Python not concatenating string and unicode to link

When I append a Unicode string to the end of str, I can not click on the URL.Bad:base_url = https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&tit…

Decrypt python/django password with Symfony 2.5 (using symfony security)

I want to use symfony 2.5.10 security in order to login in from users that were created with pyhton/django security. Passwords in db that are encrypted in this format:pbkdf2_sha256$12000$dVPTWPll8poG$3…

Reuse getCmd object in pysnmp

In the pysnmp documentation there is a getCmd class, I was wondering if it was possible to just instantiate the class once and reuse it at a later point by passing it new oids. I am not sure if the ge…

Django plugged into Apache not working like Django standalone

I have encountered a hodgepodge of errors trying to bring my django site into production with Apache. Having finally gotten mod_wsgi sorted and Apache at least seeming to be trying to load the site Im …

How to filter overlap rows in a big file in python

I am trying to filter overlap rows in a big file in python.The overlap degrees is set to 25%. In other words,the number of element of intersection between any two rows is less than 0.25 times of union …

Installing wxPython in Ubuntu 12.10

I am trying to install wxPython on my Ubuntu 12.10 but with no success. I have gone through all the answers given on this website. Can someone please help me in this or point me in the right direction.…

Open a file name +date as csv in Python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…