1. Problem DescriptionIn the course of teaching, we often need to process some text materials for creating a corpus. For example, in a folder named 【Four-Level Papers】, there are multiple folders containing past four-level exam papers, each with files like Four-Level Exam Papers.docx and Exam Paper Analysis.docx. I want to extract the files that contain the keyword 【Four-Level Exam Papers】 and place them in the corpus directory as materials for later corpus creation. How can I quickly achieve this goal? Manually, it would require opening each folder, finding the relevant files, and copying and pasting them into the specified folder, which is slow and prone to errors. This is where Python can help solve the problem.2. Thought AnalysisUsing Python, we first set the paths for the source folder and the destination folder, then use a for loop to traverse all directories, find files with the keyword 【Exam Papers】, and copy them to the corpus directory.At this point, we will use the pathlib module for path handling and the shutil module for file operations. When writing the program, we first import the modules, define the directory paths, and ensure that the corpus directory exists. For convenience, we are using relative paths here.We will use the path.rglob() method, along with the * wildcard in the filename to match files that contain the keyword.
from pathlib import Path
import shutil
# Set the directory path for the exam papers and the target directory path
source_dir = Path('./四级卷') # Change to your exam papers directory path
corpus_dir = Path('./corpus') # Current corpus directory
# Create the target directory if it does not exist
corpus_dir.mkdir(parents=True, exist_ok=True)
# Traverse the exam papers directory and subdirectories, looking for docx files with "Exam Papers"
for file in source_dir.rglob('*四级真题*.*'):
if file.is_file() and file.suffix.lower() in ('.doc', '.docx'): # Check file extension
# Copy file to corpus directory
shutil.copy(file, corpus_dir) # Copy file to corpus directory
print("File copy completed!")
The Path module plays a significant role in the entire process, as it can be used for both traversal and checking if a file ends with a specified suffix. Moreover, this module does not require installation, as it is included with Python 3.4 and later.If you are still a beginner and not very familiar with the code, you can directly describe your requirements to deepseek, especially clarifying the names and paths of the files you want to process, the tasks you want it to perform, and the final goals (including where to copy the files). For convenience, the directories traversed in the instructions are all set to the current directory, and the generated corpus is also placed in the current directory to avoid issues with absolute paths leading to files not being found.3. SummaryWith the assistance of Python and artificial intelligence, operations such as batch file creation, renaming, moving, deleting, and format conversion in our daily lives can be handled by Python. This not only improves work efficiency but also enhances the sense of achievement in learning Python.Therefore, when faced with repetitive file operations, we can try to describe our specific needs to artificial intelligence, generate the code we want, and debug it in a Thonny file. While backing up the original files, we can use the code to operate on the copied files, testing the usability of the Python code. If errors occur, there is no need to worry; we can directly feed the error messages back to the AI to find debugging solutions. As long as the description is accurate, usually only minor adjustments to the filename or indentation are needed to resolve the issue in one go.Finally, follow me, and let’s embark on the journey of learning Python together!Recommended Reading:Seven Methods for Reading TXT Text Data in PythonUsing Python for Corpus: Removing Punctuation from TextThree Small Examples of Python Programming for Humanities StudentsEnglish Thinking in Python ProgrammingWhy English Learners Should Learn PythonTransforming Bilingual Texts into Side-by-Side Tables with PythonRecommended Book: “Python for English Teachers with No Background” by Professor Zhang Wenxia from Tsinghua University