Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: list_folders_in_directory.py
- # Author: Jeoi Reqi
- """
- This Python script prompts the user to select a folder using the file manager.
- It then creates a text file listing all the files in the selected folder.
- Requirements:
- - Python 3.x
- - tkinter library (standard library, no additional installation needed)
- """
- import os
- import tkinter as tk
- from tkinter import filedialog
- def list_files_to_txt(folder_path, output_file):
- try:
- with open(output_file, 'w', encoding='utf-8') as file:
- file.write(f"List of files in folder: {folder_path}\n\n")
- for file_name in os.listdir(folder_path):
- file.write(file_name + '\n')
- print(f'Successfully created file list in: {output_file}')
- except Exception as e:
- print(f'An error occurred: {e}')
- if __name__ == "__main__":
- root = tk.Tk()
- root.withdraw() # Hide the main window
- folder_path = filedialog.askdirectory(title="Select Folder")
- if not folder_path:
- print("No folder selected. Exiting.")
- else:
- output_file = "files_list.txt"
- list_files_to_txt(folder_path, output_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement