Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 delete file with handling
#1
Hi,

So I want to delete some files and if a file is in use, the script should print an error.
However I don't care for the error report if the file doesn't exists.
So only "in use " should e raised.

(Python 2.7)

At first I thought:
try:
    os.remove(data_file)
except OSError as e:
	print("Error: file {}, {}".format(data_file, e.strerror))
But that obviously returns all errors, including 'file doesn't exist'.

my remaining option is to include a os.filepath.exists.
if os.filepath.exists(data_file):
   try:
        os.remove(data_file)
   except OSError as e:
	    print("Error: file {}, {}".format(data_file, e.strerror))
OR knowing the error of file in use (32 if I'm correct)
try:
    os.remove(data_file)
except OSError as e:
	if e.strerror.startswith("[Errno 32]"):
		print("Error: file {}, {}".format(data_file, e.strerror))
Am I right?
Quote
#2
Anybody a thought?

(sorry small bump :) )
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  delete a file after closing it mcgrim 1 216 May-14-2019, 08:16 PM
Last Post: Larz60+
  Delete multiple files using file extention sumncguy 2 416 Jan-23-2019, 03:05 AM
Last Post: snippsat
  Help with file handling gonzo620 10 1,582 Sep-27-2018, 06:28 PM
Last Post: nilamo
  Delete file with read-only permission, but write permission to parent folder cubei 4 4,618 Aug-28-2018, 09:40 AM
Last Post: cubei
  Compare element of list with line of file : if match, delete line silfer 4 783 Jul-21-2018, 02:44 PM
Last Post: silfer
  delete line from a text file thesisonews 2 16,751 Mar-20-2018, 08:30 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)