Python: Opening a file without creating a lock

2024/10/18 15:10:25

I'm trying to create a script in Python to back up some files. But, these files could be renamed or deleted at any time. I don't want my script to prevent that by locking the file; the file should be able to still be deleted at any time during the backup.

How can I do this in Python? And, what happens? Do my objects just become null if the stream cannot be read?

Thank you! I'm somewhat new to Python.

Answer

As mentioned by @kindall, this is a Windows-specific issue. Unix OSes allow deleting.

To do this in Windows, I needed to use win32file.CreateFile() to use the Windows-specific dwSharingMode flag (in Python's pywin32, it's just called shareMode).

Rough Example:

import msvcrt
import os
import win32filepy_handle = win32file.CreateFile('filename.txt',win32file.GENERIC_READ,win32file.FILE_SHARE_DELETE| win32file.FILE_SHARE_READ| win32file.FILE_SHARE_WRITE,None,win32file.OPEN_EXISTING,win32file.FILE_ATTRIBUTE_NORMAL,None
)
try:with os.fdopen(msvcrt.open_osfhandle(py_handle.handle, os.O_RDONLY)) as file_descriptor:... # read from `file_descriptor`
finally:py_handle.Close()

Note: if you need to keep the win32-file open beyond the lifetime of the file-handle object returned, you should invoke PyHandle.detach() on that handle.

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

Related Q&A

Tensorflow dataset questions about .shuffle, .batch and .repeat

I had a question about the use of batch, repeat and shuffle with tf.Dataset.It is not clear to me exactly how repeat and shuffle are used. I understand that .batch will dictate how many training exampl…

How to sort in python with multiple conditions?

I have a list with sublists as follows:result = [ [helo, 10], [bye, 50], [yeah, 5], [candy,30] ]I want to sort this with three conditions: first, by highrest integer in index 2 of sublist, then by leng…

Not able to convert Numpy array to OpenCV Mat in Cython when trying to write c++ wrapper function

I am trying to implement cv::cuda::warpPerspective in python2, there is a very sweet post about how to do that here: link. I followed the instruction as described in that post, however, I got Segmentat…

Installing python tables on mac with m1 chip

I am trying to use tables in python3 on a new mac mini with the M1 chip. I am getting multiple errors when running HDF5_DIR=/opt/homebrew/Cellar/hdf5/1.12.0_1 pip3 install tablesERROR: Command errored …

Write unbuffered on python 3

Im trying to create a file on python without buffer, so its written at the same time I use write(). But for some reason I got an error. This is the line Im using: my_file = open("test.txt", &…

which should I use (for python-based sites)? sass, compass, switchcss...alternatives? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Invalid tag name error when creating element with lxml in python

I am using lxml to make an xml file and my sample program is :from lxml import etree import datetime dt=datetime.datetime(2013,11,30,4,5,6) dt=dt.strftime(%Y-%m-%d) page=etree.Element(html) doc=etree.E…

Method replacement at runtime not updating Private attributes

I understood how to replace methods at run time in Python by going through these links.[ Link1 , Link2 , & Link3].When I replaced a "update_private_variable" method of class A, its gettin…

sys.path and sys.executable is incorrect in jupyter, but no applied fix is working

Ive configured jupyter to be used from a remote computer and set a password to it while initial anaconda setup. Then after fixing this issue, I am trapped in another one. sys.path and sys.executable is…

How to multicolour text with ScrolledText widget?

from tkinter import * from tkinter.scrolledtext import ScrolledTextwindow= Tk() window.geometry(970x45) box = ScrolledText(window, width=70, height=7).pack() box.insert(END, "Ehila") #this in…