Bottom Page

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

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:
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):
   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)
except OSError as e:
	if e.strerror.startswith("[Errno 32]"):
		print("Error: file {}, {}".format(data_file, e.strerror))
Am I right?
Anybody a thought?

(sorry small bump :) )

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Delete all contents of a file from the fifth line? PythonNPC 1 189 Apr-18-2020, 09:16 AM
Last Post: buran
  file handling sivareddy 1 209 Feb-23-2020, 07:28 PM
Last Post: jefsummers
  Can't seem to figure out how to delete several lines from a text file Cosmosso 9 528 Dec-10-2019, 11:09 PM
Last Post: Cosmosso
  delete a file after closing it mcgrim 1 380 May-14-2019, 08:16 PM
Last Post: Larz60+
  Delete multiple files using file extention sumncguy 2 661 Jan-23-2019, 03:05 AM
Last Post: snippsat
  Help with file handling gonzo620 10 2,565 Sep-27-2018, 06:28 PM
Last Post: nilamo
  Delete file with read-only permission, but write permission to parent folder cubei 4 7,879 Aug-28-2018, 09:40 AM
Last Post: cubei
  Compare element of list with line of file : if match, delete line silfer 4 991 Jul-21-2018, 02:44 PM
Last Post: silfer
  delete line from a text file thesisonews 2 22,828 Mar-20-2018, 08:30 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)