nouseforname.de

Dateien sammelen mit Python

Neulich wurde ich von einem Kollegen gefragt ob ich ihm nicht ein kleines Tool basteln kann. Er möchte verstreute Dateien auf seinem Windows System finden und diese an einen Ort kopieren. Ich habe das ganze dann direkt mal mit Python als Konsolenanwendung umgesetzt und für Windows dann natürlich noch als .exe Datei kompiliert.
Features:

import configparser
import shutil
import os
import sys

separator = os.sep
newline = os.linesep
configFile = os.getcwd() + separator + "config.ini"
config = configparser.ConfigParser()

if not os.path.isfile(configFile) or not os.access(configFile, os.R_OK):
print("**\n\tConfiguration file does not exist... created new one\n**")

if not os.path.exists("found"):
os.makedirs("found")

with open(configFile, 'w') as f:
f.write("[Configuration]\n")
f.write("\n")
f.write("; mode=copy -> copy all files\n")
f.write("; mode=move -> move all files\n")
f.write("; mode=list -> list all files only\n")
f.write("mode=list\n")
f.write("\n")
f.write("; file extension to search for\n")
f.write("extension=mp3\n")
f.write("\n")
f.write("; base folder to start search\n")
f.write("basedir=" + os.getcwd() + "\n")
f.write("\n")
f.write("; target folder to receive all copied/moved files\n")
f.write("targetdir=" + os.getcwd() + separator + "found\n")

if os.path.isfile(configFile):
print("\n\nRead config from: " + configFile)
config.read(configFile)
options = config.options("Configuration")
mode = config.get("Configuration", options[0])
ext = "." + config.get("Configuration", options[1])
basedir = config.get("Configuration", options[2])
targetdir = config.get("Configuration", options[3])
print("\tMode: " + mode);
print("\tExtension: " + ext);
print("\tBasedir: " + basedir);
print("\tTargetdir: " + targetdir);
print("")

tmp = input("Press Enter to continue...\n\n")

counter = 0
for path, subdirs, files in os.walk(basedir):
for file in files:

if path == targetdir:
continue

if file.endswith(ext):
counter += 1
print("Found: " + os.path.join(path, file))

if mode == "copy":
shutil.copy(os.path.join(path, file), targetdir)
elif mode == "move":

if os.path.isfile(os.path.join(targetdir, file)):

os.remove( os.path.join(targetdir, file))

shutil.move(os.path.join(path, file), targetdir)

print("\n\nDone. Number of files found: " + str(counter))
input("Press Enter to exit...")

Damit das ganze auch ohne Python-Installation lauffähig wird muss es noch umgewandelt werden. Dazu wird cx_freeze benötigt. Bitte beachten dass die korrekte Version installiert wird. Auf 64bit kompilierte Skripte laufen nicht auf 32bit Systemen, umgedreht ist das aber kein Problem.\r\n\r\nFür die Umwandlung zur ausführbaren .exe Datei, wird noch eine setup.py Datei benötigt:

from cx_Freeze import setup, Executable

buildOptions = dict(packages = [], excludes = [])
base = 'Console'
executables = [
Executable('movefiles.py', base=base)
]
setup(name='movefiles',
version = '0.1',
description = 'find and copy/move/list files',
options = dict(build_exe = buildOptions),
executables = executables)

Nun mit der korrekten Python-Version (32/64 bit) diese setup.py ausführen.
../python.exe setup.py build
Es entsteht ein neuer Ordner build im gleichen Verzeichniss, welcher die Bibliotheken und die .exe enthält.

#python#script#files | | #7
Zurück