2017-02-19 7 views
0
import os 
    for folder, subfolder, file in os.walk ('SomePath'): 
    for filename in file: 
     if filename.endswith('.nfo'): 
      os.unlink(path) 

Wie kann ich den absoluten Pfad der Datei finden und an os.unlink (Pfad) übergeben? .nfo-Datei kann irgendwo wie SomePath/Ordner/Unterordner/Datei sein?Dateipfad in os.walk() in Python finden

os.unlink (os.path.abspath (Dateiname)) wird nicht helfen. Wenn ich glob.glob versuche, wird nur das aktuelle Verzeichnis (Ordner bei SomePath) durchsucht.

Antwort

0
import os 
for folder, subfolder, file in os.walk('SomePath'): 
for filename in file: 
    if filename.endswith('.srt'): 
     os.unlink(os.path.join(folder,filename)) 

sieht wie oben gearbeitet aus. Wenn ich jetzt die letzte Zeile durch print (Dateiname) ersetze, bekomme ich keine Liste .srt Dateien

0

Um alle Dateien eines Verzeichnisses und/oder Unterverzeichnisse zu erhalten, verwende ich den folgenden Code in vielen meiner Projekte:

import os 
def get_all_files(rootdir, mindepth = 1, maxdepth = float('inf')): 
    """ 
    Usage: 

    d = get_all_files(rootdir, mindepth = 1, maxdepth = 2) 

    This returns a list of all files of a directory, including all files in 
    subdirectories. Full paths are returned. 

    WARNING: this may create a very large list if many files exists in the 
    directory and subdirectories. Make sure you set the maxdepth appropriately. 

    rootdir = existing directory to start 
    mindepth = int: the level to start, 1 is start at root dir, 2 is start 
       at the sub direcories of the root dir, and-so-on-so-forth. 
    maxdepth = int: the level which to report to. Example, if you only want 
       in the files of the sub directories of the root dir, 
       set mindepth = 2 and maxdepth = 2. If you only want the files 
       of the root dir itself, set mindepth = 1 and maxdepth = 1 
    """ 
    rootdir = os.path.normcase(rootdir) 
    file_paths = [] 
    root_depth = rootdir.rstrip(os.path.sep).count(os.path.sep) - 1 
    for dirpath, dirs, files in os.walk(rootdir): 
     depth = dirpath.count(os.path.sep) - root_depth 
     if mindepth <= depth <= maxdepth: 
      for filename in files: 
       file_paths.append(os.path.join(dirpath, filename)) 
     elif depth > maxdepth: 
      del dirs[:] 
    return file_paths 

Danach können Sie filtern, schneiden und würfeln, wie auch immer Sie wollen. Wenn Sie innerhalb dieser Routine filtern möchten, ist der Ort, um es zu tun, nach for filename in files: und vor file_paths.append(os.path.join(dirpath, filename))

HTH.