Verzeichnisverarbeitung in Python
- Wiedergabe eines zufälligen Songs
import glob
import os
import pathlib
import random
mediaplayer = r'C:\Program Files\mplayer\mplayer.exe'
dirs = r'C:\example', r'C:\example1'
ending = '.mp3'
def commandsyntax( mediaplayer, song ):
return fr'""{mediaplayer}" "{song}""'
list = []
for dir in dirs:
for name in pathlib.Path( dir ).glob( r'**/*'+ending ):
list.append( name )
song = random.choice( list )
print( song )
command = commandsyntax( mediaplayer, song )
print( command )
os.system( command )
- Notizen
import pathlib
a_directory_containing_the_files_to_be_added = r"C:\example"
path = pathlib.Path( a_directory_containing_the_files_to_be_added )
a_glob_for_the_files_to_be_added = "**\*.txt"
files = path.glob( a_glob_for_the_files_to_be_added )
for file in files: