Python Forum
Cannot open file in an OpenSharedItem() iterator - The file may not exist/may be open
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot open file in an OpenSharedItem() iterator - The file may not exist/may be open
#1
I am trying to run the following code in Python:

for filename in os.listdir(workdir1):
msg=outlook.OpenSharedItem(workdir1 + filename)
if str(msg.Body).find("TestString"):
myfile = open(workdir + "results.txt", 'w')
myfile.write("%s\n" % filename)
myfile.close()
However, I keep getting the following error:

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Cannot open file: N:\emails\TEST1.msg. The file may not exist, you may not have permission to open it, or it may be open in another program. Right-click the folder that contains the file, and then click Properties to check your permissions for the folder.', None, 0, -2147287008), None)

Note that TEST1.msg does exist, but when I try to delete it from the directory I get 'file opened in another program (Python)' error. Seems like that file is constantly opened...

How can I run my code whilst remediating this issue (e.g. iterate over every file in the test directory and write results of a search function)?

Thanks a lot!
Reply
#2
Please, edit your post using the tags (python code, output and error).
With tags it becomes more clear for us to help you.
Reply
#3
Apologies, I am new to these boards and hence lack proper syntax...

for filename in os.listdir(workdir1):
msg=outlook.OpenSharedItem(workdir1 + filename)
if str(msg.Body).find("TestString"):
myfile = open(workdir + "results.txt", 'w')
myfile.write("%s\n" % filename)
myfile.close()
Error:
com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Cannot open file: N:\emails\TEST1.msg. The file may not exist, you may not have permission to open it, or it may be open in another program. Right-click the folder that contains the file, and then click Properties to check your permissions for the folder.', None, 0, -2147287008), None)
Reply
#4
I think the error is here:

msg=outlook.OpenSharedItem(workdir1 + filename)
Check if the path workdir1 + filename is really the path of the wanted file. Try to add '/' between them.
Another thing is that you use workdir1 and then workdir. Is this right?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 278 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 939 Dec-14-2023, 08:03 AM
Last Post: shanoger
  UndefinedEnvironmentName: 'extra' does not exist in evaluation environment EarthAndMoon 3 1,620 Oct-09-2023, 05:38 PM
Last Post: snippsat
  how to open a popup window in tkinter with entry,label and button lunacy90 1 810 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  Need to replace a string with a file (HTML file) tester_V 1 698 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  QUIZ GUI Form_When admin panel is open, main quiz form is getting freeze Uday 4 707 Aug-25-2023, 08:24 PM
Last Post: deanhystad
  open python files in other drive akbarza 1 632 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 639 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  How can i combine these two functions so i only open the file once? cubangt 4 804 Aug-14-2023, 05:04 PM
Last Post: snippsat
  How can I change the uuid name of a file to his original file? MaddoxMB 2 868 Jul-17-2023, 10:15 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020