[파이썬] 파일의 디렉토리 내 파일 합치기

In this blog post, we will explore how to merge files within a directory using Python. This can be useful when dealing with large datasets split into multiple files, or when you want to consolidate multiple text files into a single document.

Approach

To merge files within a directory, we will use the following steps:

  1. Identify the target directory where the files are located.
  2. Get the list of all files within the directory.
  3. Open a new file for writing.
  4. Loop through each file in the directory.
  5. Open each file one by one and read its content.
  6. Write the content to the new file.
  7. Close all files.

Let’s dive deeper into each step and see how to implement it in Python.

Step 1: Identify the Target Directory

To specify the target directory, we can either hardcode the path or provide it as an input to the script.

import os

# Hardcoded directory path
directory = '/path/to/directory'

# Prompt user to input directory path
directory = input("Enter the directory path: ")

Step 2: Get List of Files

We can use the os.listdir() function to get a list of all files within the directory.

files = os.listdir(directory)

Step 3: Open a New File

Next, we need to create a new file where we will merge all the content. We can use the open() function with the mode='w' parameter to open the file in write mode.

new_file = open('merged_file.txt', 'w')

Step 4: Loop through Files

Now, we can iterate through each file in the directory using a for loop.

for file_name in files:
    file_path = os.path.join(directory, file_name)

    # Open each file in read mode
    with open(file_path, 'r') as file:
        # Step 5: Read the content of the file
        content = file.read()

        # Step 6: Write the content to the new file
        new_file.write(content)

    # Step 7: Close the file
    file.close()

Step 5: Read and Write Content

Within the loop, we open each file in read mode using the with open() statement. We then read the content of the file using the read() function and write it to the new file using the write() function.

Step 6: Close Files

Finally, we need to close all the files to release system resources.

new_file.close()

Conclusion

By following the steps outlined in this article, you can easily merge files within a directory using Python. This technique can be applied in various scenarios where data consolidation is required. Experiment with different file types, such as CSV or JSON, and customize the code to suit your specific needs.

Remember to handle errors and exceptions appropriately, considering edge cases and potential issues with file permissions.