Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

modul:m122:learningunits:lu08:loesungen:dateien [2024/12/16 09:04] – angelegt msutermodul: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):
-    """ Creates a new directory """ +    """ 
-    pass +    Creates a directory with the specified name. 
-    +    :param directory_name: 
 +    :return: 
 +    """ 
 +    try: 
 +        result = subprocess.run( 
 +            'mkdir ./python_bash_test&&cd python_bash_test', 
 +            shell=True,  # Use a shell to interpret the command 
 +            check=True,  # Raise an error if the command fails 
 +            text=True,  # Capture output as a string 
 +            stdout=subprocess.PIPE,  # Redirect standard output 
 +            stderr=subprocess.PIPE  # Redirect standard error 
 +        ) 
 +    except subprocess.CalledProcessError as e: 
 +        print(f'Error executing the command: {e.cmd}'
 +        print(f'Error message: {e.stderr}'
 +        sys.exit(1) 
 +    except Exception as ex: 
 +        print(f'An unexpected error occurred: {ex}'
 +        sys.exit(1) 
  
 def create_files(directory_name, file_names): def create_files(directory_name, file_names):
-    """ Creates all files in the list """ +    """ 
-    pass+    Creates files with the specified names in the specified directory. 
 +    :param directory_name: 
 +    :param file_names: 
 +    :return: 
 +    """ 
 +    for filename in file_names: 
 +        try: 
 +            result = subprocess.run( 
 +                f'touch ./{directory_name}/' + filename, 
 +                shell=True,  # Use a shell to interpret the command 
 +                check=True,  # Raise an error if the command fails 
 +                text=True,  # Capture output as a string 
 +                stdout=subprocess.PIPE,  # Redirect standard output 
 +                stderr=subprocess.PIPE  # Redirect standard error 
 +            ) 
 +        except subprocess.CalledProcessError as e: 
 +            print(f'Error executing the command: {e.cmd}'
 +            print(f'Error message: {e.stderr}'
 +            sys.exit(1) 
 +        except Exception as ex: 
 +            print(f'An unexpected error occurred: {ex}'
 +            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 ./{directory_name}', 
 +            shell=True,  Use a shell to interpret the command 
 +            check=True,  # Raise an error if the command fails 
 +            text=True,  # Capture output as a string 
 +            stdout=subprocess.PIPE,  # Redirect standard output 
 +            stderr=subprocess.PIPE  # Redirect standard error 
 +        ) 
 +        print(result.stdout) 
 +    except subprocess.CalledProcessError as e: 
 +        print(f'Error executing the command: {e.cmd}'
 +        print(f'Error message: {e.stderr}'
 +        sys.exit(1) 
 +    except Exception as ex: 
 +        print(f'An unexpected error occurred: {ex}'
 +        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'du -h ./{directory_name}', 
 +            shell=True,  # Use a shell to interpret the command 
 +            check=True,  # Raise an error if the command fails 
 +            text=True,  # Capture output as a string 
 +            stdout=subprocess.PIPE,  # Redirect standard output 
 +            stderr=subprocess.PIPE  # Redirect standard error 
 +        ) 
 +        print(result.stdout) 
 +    except subprocess.CalledProcessError as e: 
 +        print(f'Error executing the command: {e.cmd}'
 +        print(f'Error message: {e.stderr}'
 +        sys.exit(1) 
 +    except Exception as ex: 
 +        print(f'An unexpected error occurred: {ex}'
 +        sys.exit(1) 
 + 
 +def delete_files(directory_name, filename): 
 +    """ 
 +    Deletes files with the specified names in the specified directory. 
 +    :param directory_name: 
 +    :param filename: 
 +    :return: 
 +    """ 
 + 
 +    try: 
 +        result = subprocess.run( 
 +            f'rm ./{directory_name}/' + filename, 
 +            shell=True,  # Use a shell to interpret the command 
 +            check=True,  # Raise an error if the command fails 
 +            text=True,  # Capture output as a string 
 +            stdout=subprocess.PIPE,  # Redirect standard output 
 +            stderr=subprocess.PIPE  # Redirect standard error 
 +        ) 
 +    except subprocess.CalledProcessError as e: 
 +        print(f'Error executing the command: {e.cmd}'
 +        print(f'Error message: {e.stderr}'
 +        sys.exit(1) 
 +    except Exception as ex: 
 +        print(f'An unexpected error occurred: {ex}'
 +        sys.exit(1)
  
 if __name__ == '__main__': if __name__ == '__main__':
     directory = 'python_bash_test'     directory = 'python_bash_test'
     files = ['file1.txt', 'file2.txt', 'file3.txt']     files = ['file1.txt', 'file2.txt', 'file3.txt']
-    +
     create_directory(directory)     create_directory(directory)
     create_files(directory, files)     create_files(directory, files)
-    # call functions for step 3-5 +    list_files(directory) 
-</code>+    file_size(directory) 
 +    delete_files(directory, 'file3.txt')
  
  
  
-===== Erwartete Ergebnisse ===== 
  
- 
-Beim Ausführen des Skripts sollte die Konsole folgendes ausgeben: 
- 
-==== Nach Schritt 1: ==== 
- 
-<code> 
-   Verzeichnis 'python_bash_test' erfolgreich erstellt. 
 </code> </code>
  
-==== Nach Schritt 2: ==== 
- 
-<code> 
-   Datei 'python_bash_test/file1.txt' erfolgreich erstellt. 
-   Datei 'python_bash_test/file2.txt' erfolgreich erstellt. 
-   Datei 'python_bash_test/file3.txt' erfolgreich erstellt. 
-</code> 
- 
-==== Nach Schritt 3 (Liste der Dateien): ==== 
- 
-<code> 
-   Dateien im Verzeichnis 'python_bash_test': 
-   file1.txt 
-   file2.txt 
-   file3.txt 
-</code> 
- 
-==== Nach Schritt 4 (Dateigröße): ==== 
- 
-<code> 
-   Größen der Dateien im Verzeichnis 'python_bash_test': 
-   0B file1.txt 
-   0B file2.txt 
-   0B file3.txt 
-</code> 
- 
-==== Nach Schritt 5 (Aktualisierte Liste nach Löschung): ==== 
- 
-<code> 
-   Datei 'python_bash_test/file3.txt' wurde gelöscht. 
-   Aktualisierte Dateien im Verzeichnis: 
-   file1.txt 
-   file2.txt 
-</code> 
  
 ---- ----
 {{tag>M122-LU08}} {{tag>M122-LU08}}
 [[https://creativecommons.org/licenses/by-nc-sa/4.0/|{{https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png}}]] Marcel Suter [[https://creativecommons.org/licenses/by-nc-sa/4.0/|{{https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png}}]] Marcel Suter
  • modul/m122/learningunits/lu08/loesungen/dateien.1734336280.txt.gz
  • Zuletzt geändert: 2024/12/16 09:04
  • von msuter