Python Forum

Full Version: Issue with os.rename
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello experts,

I am facing this issue with os.rename

I have multiple scripts in my Jupyter notebook, and at some point I need to move some files to the "processed" folder.
When I run below code
import os
post_id='66ec21db97'
for s,n in majle.items():
    if n==post_id:
        d = re.sub(r'CCC\\',r'CCC\\processed\\',s)
        print('src - {}'.format(s))  
        print('dst - {}'.format(d))        
#         os.rename(s, d)
I am getting correct output:

src - C:\Users\marcin\Desktop\CCC\_External_ New Topic_ _Visio process map for customer account creation_ in Receivables & Collections.msg
dst - C:\Users\marcin\Desktop\CCC\processed\_External_ New Topic_ _Visio process map for customer account creation_ in Receivables & Collections.msg

But when I uncomment last line - os.rename(s, d), I am getting this error with double backslashes in my path:

Error:
PermissionError Traceback (most recent call last) <ipython-input-11-b0460c8bfdbc> in <module> 6 print('src - {}'.format(s)) 7 print('dst - {}'.format(d)) ----> 8 os.rename(s, d) PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\marcin\\Desktop\\CCC\\_External_ New Topic_ _Visio process map for customer account creation_ in Receivables & Collections.msg' -> 'C:\\Users\\marcin\\Desktop\\CCC\\processed\\_External_ New Topic_ _Visio process map for customer account creation_ in Receivables & Collections.msg'
Is this the issue with backshales or some previous script indeed is blocking the file?
If so, how to release the the file?
Is another program generating the file? Do you have it open anywhere (such as Excel)?
This is *.msg file - email saved from Outlook.
I closed Outlook, closed Total Commander but still getting the issue :(

I am using this to get some information from email body

import extract_msg

msg = extract_msg.Message(ff)
msg_message = msg.body
p_id = msg_message[msg_message.find(start_text)+len(start_text):msg_message.find(end_text)]
Maybe this code is holding the file open?

Any ideas?
After more testing I am sure that one of below guys is holding the file:
msg = extract_msg.Message(ff)
msg_message = msg.body
But still I have not idea how to release it.

It's reported bug:

https://github.com/mattgwwalker/msg-extr...-container
FYI,

In my case it was an issue with files which already existed in the destination folder.
It's working fine with
from shutil import move
which overrides existing files.