I am trying to merge two PDFs using PyPDF2. Sometimes I run into the following error:
[WinError 32] The process cannot access the file because it is being used by another process:
How can I determine which process is using the file?
Thank you
You might have already tried this, but try to find the handle or the DLL ; a dialogue box will open and allow you to search for the missing file. If you want to open a file in Python, search up a file handling cheat-sheet. It doesn't require any importing of modules, just the Open() function. Glad to help if i did
Related
This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Closed 6 months ago.
I have again problems with that famous Exception.
The problem is the following:
I have wanted to write a simple code for an easy exercise about files and excel libraries (openpyxl) that I have to be make for my python programming course. At first, I have to say that I usually make all my codes in iOS.
For this time, I have wanted to do this exercise in my old PC whose O.S. is Windows, so I have exported the file where I work in iOS in a pen drive and next, export that file in my Windows desktop.
I opened VSC and the first code's line is the problem because I try to load the excel file but the exception FileNotFoundError appears.
As simple as write:
import openpyxl
fw = openpyxl.load_workbook("Data_File_2.xlsx")
The file when I'm writing the code is in the follow route: C:\Users\xxxx\Desktop\pyWork
which is the same route where the file "Data_File_2" is in it.
Well, the problem continues because every time I want to write a new code for working with extern files, this Exception appears again, even with other 'py' files that I made where I worked with extern files and now I can't run because this Exception appears again and block the program. (I'm talking about old files made in previously days or weeks which works correctly with external files)
For example, now I'm trying to open a try file .txt from a new py.file, both of them located in the same directory:
with open("try_to_open.txt","r") as op_file:
rfile = op_file.read()
print(rfile)
#The exception FileNotFoundError appears again in the first line.
I know that I can use try/else blocks but I want to know why this Exception appears, what is the Error in this case and why it appears every time I want to work with external file one time that the Exception appears at first.
I think I have found a solution but I dont know if it's correct for similar situations like this. I was able to resolve by writing as follows:
import openpyxl
file_work = openpyxl.load_workbook("Pyworks\\Data_File_2.xlsx")
Hi I am checking to see if a excel file is modified, and if it is basically save it as something else and open it. So it works the first time around, but on the second time I modify the file, I am getting error: The system cannot find the file specified: 'C:\example.xlsx'
Sometimes it would also throw: Permission denied: 'C:\Todolist2.xlsx'
Please help. Newbie here. Thank You
import time, os.path, os, openpyxl
from openpyxl import Workbook
currentFD = os.stat("C:\\example.xlsx")
while True:
modDate = os.stat("C:\\example.xlsx")
if (modDate > currentFD):
print('yes it does')
wb=openpyxl.load_workbook("C:\\example.xlsx")
wb.save("C:\\Todolist2.xlsx")
os.startfile("C:\\Todolist2.xlsx")
currentFD = modDate
You seem to have two different problems here but they may be related.
Since you gave a traceback for the Permission Denied error on C:\Todolist2.xlsx, let's look at that one.
On Windows, many programs, when they open a file, put a lock on it. This is especially true for "applications" programs, like Excel and Notepad. If one program has a file locked, any another program that tries to open that file to overwrite it will get a permissions error.
And that's exactly what you're seeing: The first time through, your code overwrites Todolist2.xslx, then uses startfile to tell Excel (or some application that's registered for Excel files) to open it, which works. Then it tries to overwrite the same file, which Excel presumably still has locked and open, which fails.
Depending on what you're actually trying to do here, there are a few possible workarounds:
Copy Todolist2.xlsx to a temporary file, then start that temporary.
Create new files Todolist2.xlsx, Todolist2-1.xlsx, etc., and keep opening them.
Use either COM automation or a GUI scripting framework like autogui to make Excel open a copy of the file rather than opening the file itself.
Use either of the above to make Excel close the file before overwriting the file and launching it.
Launch a new Excel instance to open the file using subprocess.Popen, so you can kill it and launch a new one.
Rewrite your whole code to build the spreadsheet using Excel COM automation, rather than by building a file to pass to it.
I am working in Windows, and just learning to use python (python 2.7).
I have a bunch of script files ("file1.script", "file2.script", "file3.script"....) that are executed in TheProgram.exe. Python has already given me the ability to automatically create these script files, but now I want to successively run each of these script files, back-to-back, in TheProgram.exe.
So far I have figured out how to use the subprocess module in python to start "TheProgram.exe" in a new process (child process?) and load the first script file as follows:
my_process = subprocess.Popen(["Path to TheProgram.exe", "Path to File1.script"])
As seen, simply "opening" the script file in TheProgram.exe, or passing it as an argument in this case, will execute it. Once File1.script is done, TheProgram.exe generates an output file, and then just sits there. It does not terminate. This is I want, because now I would like to load File2.script in the same process without terminating (file2.script is dependent on file1.script completing successfully), then File3.script etc.
Is this possible? And if so how? I cannot seem to find any documentation or anyone else who has had this problem. If I can provide other information please let me know, I am also new to posting to these forums. Thanks so much for any assistance.
I am using Python to develop an application that does the following:
Monitors a particular directory and watches for file to be
transferred to it. Once the file has finished its transfer, run some
external program on the file.
The main issue I have developing this application is knowing when the file has finished transferring. From what I know the file will be transferred via SFTP to a particular directory. How will Python know when the file has finished transferring? I know that I can use the st_size attribute from the object returned by os.stat(fileName) method. Are there more tools that I need to use to accomplish these goals?
I ended up using a combination of watchdog and waited until I can open the file for writing
#If there is no error when trying to read the file, then it has completely loaded
try:
with io.FileIO(fileName, "r+") as fileObj:
'''
Deal with case where FTP client uses extensions such as ".part" and '.filepart" for part of the incomplete downloaded file.
To do this, make sure file exists before adding it to list of completedFiles.
'''
if(os.path.isfile(fileName)):
completedFiles.append(fileName)
print "File=" + fileName + " has completely loaded."
except IOError as ioe:
print str(ioe)
The best way to solve this would be to have the sending process SFTP to a holding area, and then (presumably using SSH) execute a mv command to move the file from the holding area to the final destination area. Then, once the file appears in the destination area, your script knows that it is completely transferred.
i'm having some trouble figuring out how to save unicode into a file in python. I have the following code, and if i run it in a script test.py, it should create a new file called priceinfo.txt, and write what's in price_info to the file. But i do not see the file, can anyone enlighten me on what could be the problem?
Thanks a lot!
price_info = u'it costs \u20ac 5'
f = codecs.open('priceinfo.txt','wb','utf-8')
f.write(price_info)
f.close()
I can think of several reasons:
the file gets created, but in a different directory. Be certain what the working
directory of the script is.
you don't have permission to create the file, in the directory where you want to create it.
you have some error in your Python script, and it does not get executed at all.
To find out which one it is, run the script in a command window, and check for any error output that you get.
Assuming no error messages from the program (which would be the result of forgetting to import the codecs module), are you sure you're looking in the right place? That code writes priceinfo.txt in the current working directory (IOW are you sure that you're looking inside the working directory?)