简体   繁体   中英

Check if files are open (because of error: “text file busy”) in Python

I have a big pcap file, from which I filter out some packets with tshark, save them in new pcap files and put the filenames of the new pcap files in an array called "filename_array". At the end I delete all the files which are saved in the filename_array. But one file from this array is still open when the delete process starts and due to that the error "text file busy" occurs.

So, a solution would be, to check, if all files of the array are closed. If it's true, I can delete all of them. How can I do this?

You can check for exception while reading or deleting the file and handle it.

try:
    with open('filename'):
        #include the file to be deleted in array
except IOError:
    # skip the file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM