Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a file without an extension?

Tags:

python

I have made a function for deleting files:

def deleteFile(deleteFile):
    if os.path.isfile(deleteFile):
        os.remove(deleteFile)

However, when passing a FIFO-filename (without file-extension), this is not accepted by the os-module. Specifically I have a subprocess create a FIFO-file named 'Testpipe'. When calling:

os.path.isfile('Testpipe')

It results to False. The file is not in use/open or anything like that. Python runs under Linux.

How can you correctly delete a file like that?

like image 605
Orpedo Avatar asked Oct 12 '16 12:10

Orpedo


1 Answers

isfile checks for regular file.

You could workaround it like this by checking if it exists but not a directory or a symlink:

def deleteFile(filename):
    if os.path.exists(filename) and not os.path.isdir(filename) and not os.path.islink(filename):
        os.remove(filename)
like image 141
Jean-François Fabre Avatar answered Oct 02 '22 22:10

Jean-François Fabre