Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
modul:m122:learningunits:lu08:loesungen:dateien [2024/12/16 09:04] – angelegt msuter | modul:m122:learningunits:lu08:loesungen:dateien [2024/12/16 09:31] (aktuell) – msuter | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== LU08.L01: Arbeiten mit Dateien und Verzeichnissen ====== | ====== LU08.L01: Arbeiten mit Dateien und Verzeichnissen ====== | ||
- | |||
- | |||
- | Hier ist der Aufbau des Skripts als Orientierungshilfe: | ||
- | |||
<code python> | <code python> | ||
import subprocess | import subprocess | ||
+ | import sys | ||
+ | |||
def create_directory(directory_name): | def create_directory(directory_name): | ||
- | """ | + | """ |
- | | + | |
- | | + | :param directory_name: |
+ | :return: | ||
+ | | ||
+ | | ||
+ | result = subprocess.run( | ||
+ | 'mkdir ./ | ||
+ | shell=True, | ||
+ | check=True, | ||
+ | text=True, | ||
+ | stdout=subprocess.PIPE, | ||
+ | stderr=subprocess.PIPE | ||
+ | ) | ||
+ | | ||
+ | print(f' | ||
+ | print(f' | ||
+ | sys.exit(1) | ||
+ | except Exception as ex: | ||
+ | print(f' | ||
+ | sys.exit(1) | ||
def create_files(directory_name, | def create_files(directory_name, | ||
- | """ | + | """ |
- | | + | |
+ | :param directory_name: | ||
+ | :param file_names: | ||
+ | :return: | ||
+ | | ||
+ | | ||
+ | try: | ||
+ | result = subprocess.run( | ||
+ | f' | ||
+ | shell=True, | ||
+ | check=True, | ||
+ | text=True, | ||
+ | stdout=subprocess.PIPE, | ||
+ | stderr=subprocess.PIPE | ||
+ | ) | ||
+ | except subprocess.CalledProcessError as e: | ||
+ | print(f' | ||
+ | print(f' | ||
+ | sys.exit(1) | ||
+ | except Exception as ex: | ||
+ | print(f' | ||
+ | sys.exit(1) | ||
- | # Functions for steps 3-5 | + | |
+ | def list_files(directory_name): | ||
+ | """ | ||
+ | List the files in the specified directory. | ||
+ | :param directory_name: | ||
+ | :return: | ||
+ | """ | ||
+ | try: | ||
+ | result = subprocess.run( | ||
+ | f'ls ./ | ||
+ | shell=True, | ||
+ | check=True, | ||
+ | text=True, | ||
+ | stdout=subprocess.PIPE, | ||
+ | stderr=subprocess.PIPE | ||
+ | ) | ||
+ | print(result.stdout) | ||
+ | except subprocess.CalledProcessError as e: | ||
+ | print(f' | ||
+ | print(f' | ||
+ | sys.exit(1) | ||
+ | except Exception as ex: | ||
+ | print(f' | ||
+ | sys.exit(1) | ||
+ | def file_size(directory_name): | ||
+ | """ | ||
+ | Shows the size of the files in the specified directory. | ||
+ | :param directory_name: | ||
+ | :return: | ||
+ | """ | ||
+ | try: | ||
+ | result = subprocess.run( | ||
+ | f' | ||
+ | shell=True, | ||
+ | check=True, | ||
+ | text=True, | ||
+ | stdout=subprocess.PIPE, | ||
+ | stderr=subprocess.PIPE | ||
+ | ) | ||
+ | print(result.stdout) | ||
+ | except subprocess.CalledProcessError as e: | ||
+ | print(f' | ||
+ | print(f' | ||
+ | sys.exit(1) | ||
+ | except Exception as ex: | ||
+ | print(f' | ||
+ | sys.exit(1) | ||
+ | |||
+ | def delete_files(directory_name, | ||
+ | """ | ||
+ | Deletes files with the specified names in the specified directory. | ||
+ | :param directory_name: | ||
+ | :param filename: | ||
+ | :return: | ||
+ | """ | ||
+ | |||
+ | try: | ||
+ | result = subprocess.run( | ||
+ | f'rm ./ | ||
+ | shell=True, | ||
+ | check=True, | ||
+ | text=True, | ||
+ | stdout=subprocess.PIPE, | ||
+ | stderr=subprocess.PIPE | ||
+ | ) | ||
+ | except subprocess.CalledProcessError as e: | ||
+ | print(f' | ||
+ | print(f' | ||
+ | sys.exit(1) | ||
+ | except Exception as ex: | ||
+ | print(f' | ||
+ | sys.exit(1) | ||
if __name__ == ' | if __name__ == ' | ||
directory = ' | directory = ' | ||
files = [' | files = [' | ||
- | | + | |
create_directory(directory) | create_directory(directory) | ||
create_files(directory, | create_files(directory, | ||
- | | + | |
- | </ | + | |
+ | delete_files(directory, | ||
- | ===== Erwartete Ergebnisse ===== | ||
- | |||
- | Beim Ausführen des Skripts sollte die Konsole folgendes ausgeben: | ||
- | |||
- | ==== Nach Schritt 1: ==== | ||
- | |||
- | < | ||
- | | ||
</ | </ | ||
- | ==== Nach Schritt 2: ==== | ||
- | |||
- | < | ||
- | Datei ' | ||
- | Datei ' | ||
- | Datei ' | ||
- | </ | ||
- | |||
- | ==== Nach Schritt 3 (Liste der Dateien): ==== | ||
- | |||
- | < | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | </ | ||
- | |||
- | ==== Nach Schritt 4 (Dateigröße): | ||
- | |||
- | < | ||
- | | ||
- | 0B file1.txt | ||
- | 0B file2.txt | ||
- | 0B file3.txt | ||
- | </ | ||
- | |||
- | ==== Nach Schritt 5 (Aktualisierte Liste nach Löschung): ==== | ||
- | |||
- | < | ||
- | Datei ' | ||
- | | ||
- | | ||
- | | ||
- | </ | ||
---- | ---- | ||
{{tag> | {{tag> | ||
[[https:// | [[https:// |