Ejemplo n.º 1
0
def deleteTheFile(directoryPath, nameOfFile, fileExtension):
    '''
	Removes all files corresponding to the given name and the specified file(s) extension(s).
	'''
    #if the path is correctly written at the end
    if directoryPath[-1] != u'/':
        directoryPath = u'%s/' % (directoryPath)

    #preparing to dump into file
    for char in [
            u' ', u'_', u'/', u'\\', u':', u'…', u'。', u';', u',', u'.', u'>',
            u'<', u'?', u'!', u'*', u'+', u'(', u')', u'[', u']', u'{', u'}',
            u'"', u"'", u'='
    ]:
        nameOfFile = nameOfFile.replace(char, u'_')
    #we change the iri code if there is one
    if u'%' in nameOfFile or '%' in nameOfFile:
        nameOfFile = iriToUri(nameOfFile)

    #we make a list of all the possible names of the files to be deleted
    fileNamesToBeDeleted = []
    namePlusExt = u'%s.%s' % (nameOfFile, fileExtension)

    fileNamesToBeDeleted.append(namePlusExt)
    fileNamesToBeDeleted.append(noTroublesomeName(namePlusExt))
    for nb in range(10):
        #for python 2
        try:
            strNb = unicode(nb)
        #for python 3
        except NameError:
            strNb = str(nb)
        fileNamesToBeDeleted.append(u'%s_%s.%s' %
                                    (nameOfFile, strNb, fileExtension))
        fileNamesToBeDeleted.append(
            u'%s_%s.%s' %
            (noTroublesomeName(nameOfFile), strNb, fileExtension))

    #we make a list of all extension-like
    try:
        #we catch all corresponding names
        if type(fileExtension) is str:
            filelist = [
                utilsString.toUtf8(file) for file in os.listdir(directoryPath)
                if file.endswith(".%s" % (fileExtension))
            ]
        elif type(fileExtension) or unicode:
            filelist = [
                utilsString.toUtf8(file) for file in os.listdir(directoryPath)
                if file.endswith(".%s" % (fileExtension.encode('utf8')))
            ]
    except OSError:
        filelist = []

    #we make a list of the intersection between the 2 lists
    intersection = list(set(fileNamesToBeDeleted) & set(filelist))
    #we delete the files
    for file in intersection:
        os.remove(directoryPath + file)
    return
Ejemplo n.º 2
0
def theFileExists(directoryOrWholeFilePath,
                  nameOfFile=None,
                  fileExtension=None):
    '''
	Returns false if the file does not exists at the directory
	and returns true if the file exists
	'''
    import utilsString
    #if the directory path is actually the file path
    if nameOfFile == None and fileExtension == None:
        return os.path.isfile(directoryOrWholeFilePath)
    #if the path is correctly written at the end
    if directoryOrWholeFilePath[-1] != u'/':
        directoryOrWholeFilePath = u'%s/' % (directoryOrWholeFilePath)
    #all extensions
    if fileExtension == None:
        filelist = os.listdir(directoryOrWholeFilePath)
        for file in filelist:
            splittedFileName = file.split('.')
            #if there was more than one '.'
            if len(splittedFileName) > 2:
                splittedFileName = [
                    '.'.join(splittedFileName[:len(splittedFileName) - 1])
                ]
            #if the file exists
            for nb in range(100):
                #for python 2
                try:
                    strNb = unicode(nb)
                #for python 3
                except NameError:
                    strNb = str(nb)
                if u'%s_%s' % (nameOfFile, strNb) == utilsString.toUtf8(
                        splittedFileName[0]) or u'%s_%s' % (noTroublesomeName(
                            nameOfFile), strNb) == utilsString.toUtf8(
                                splittedFileName[0]):
                    return True

        #if the file never appeared
        return False
    #exclusive extension
    else:
        return os.path.isfile(
            u'%s%s.%s' % (directoryOrWholeFilePath, nameOfFile, fileExtension))