|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +authors: ["devcrypted"] |
| 4 | +media_subpath: /assets/img/ |
| 5 | +pin: false |
| 6 | + |
| 7 | +# Should be changed according to post |
| 8 | +published: true |
| 9 | +title: "Automate Excel & CSV Data Processing with Python" |
| 10 | +permalink: automate-excel-csv-data-processing-with-python |
| 11 | +date: 2025-10-15 19:44 |
| 12 | +categories: ["Automation"] |
| 13 | +tags: ["Python", "Automation", "Tutorial"] |
| 14 | +description: Unlock efficiency with Python automation: a specific how-to guide with actionable steps to streamline your workflows. |
| 15 | +--- |
| 16 | + |
| 17 | +<!-- This blog post was automatically generated using AI --> |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Python File Automation: Copy, Move, Delete |
| 22 | + |
| 23 | +Automate file system tasks efficiently using Python's `os` and `shutil` modules. |
| 24 | + |
| 25 | +### Setup & Basics |
| 26 | + |
| 27 | +- Import modules: `import os`, `import shutil` |
| 28 | +- Current working directory: `os.getcwd()` |
| 29 | +- Change directory: `os.chdir('path/to/dir')` |
| 30 | + |
| 31 | +### File Operations |
| 32 | + |
| 33 | +- **Copy a file:** |
| 34 | + ```python |
| 35 | + shutil.copy('source.txt', 'destination.txt') |
| 36 | + # Destination can be a new name or existing directory |
| 37 | + ``` |
| 38 | +- **Move/Rename a file:** |
| 39 | + ```python |
| 40 | + shutil.move('old_name.txt', 'new_location/new_name.txt') |
| 41 | + # Renames if new_location is same as old |
| 42 | + ``` |
| 43 | +- **Delete a file:** |
| 44 | + ```python |
| 45 | + os.remove('file_to_delete.txt') |
| 46 | + ``` |
| 47 | + |
| 48 | +### Directory Operations |
| 49 | + |
| 50 | +- **Create a directory:** |
| 51 | + ```python |
| 52 | + os.mkdir('new_folder') |
| 53 | + # Creates single directory |
| 54 | + os.makedirs('path/to/new/nested/folder') |
| 55 | + # Creates all intermediate directories |
| 56 | + ``` |
| 57 | +- **List directory contents:** |
| 58 | + ```python |
| 59 | + os.listdir('.') |
| 60 | + # Returns list of names |
| 61 | + ``` |
| 62 | +- **Delete an empty directory:** |
| 63 | + ```python |
| 64 | + os.rmdir('empty_folder') |
| 65 | + ``` |
| 66 | +- **Delete non-empty directory (CAUTION!):** |
| 67 | + ```python |
| 68 | + shutil.rmtree('folder_with_content') |
| 69 | + # Recursively deletes directory and all contents |
| 70 | + ``` |
| 71 | + |
| 72 | +### Path Management |
| 73 | + |
| 74 | +- Use `os.path.join()` for OS-agnostic path construction. |
| 75 | +- Example: `file_path = os.path.join('data', 'reports', 'report.pdf')` |
0 commit comments