求助:批量将.out文件转为.txt的自动化方法(Cmd/PowerShell/Python)
Hey there! Converting 100 .out files one by one with WordPad sounds like a total time-suck. No worries—here are three easy, automated ways to get this done using Cmd, PowerShell, or Python, depending on what you're most comfortable with.
If your .out files are plain text (which they seem to be, since you can open them in WordPad), renaming them to .txt is all you need. This is the fastest method for simple conversions:
- Open Cmd and navigate to the folder where your .out files are stored (use
cd path\to\your\folder). - Run this command:
for %f in (*.out) do ren "%f" "%~nf.txt"
%frepresents each .out file in the folder.%~nfgrabs the filename without the .out extension, so we can append .txt to it.
Note: If you're putting this in a batch file, use %%f instead of %f.
PowerShell offers a more flexible approach if you need to filter files or add extra logic later:
- Open PowerShell and go to your file folder (use
cd path\to\your\folder). - Run this command:
Get-ChildItem -Filter *.out | Rename-Item -NewName { $_.BaseName + '.txt' }
Get-ChildItem -Filter *.outlists all .out files in the directory.Rename-Itemchanges each file's extension—$_.BaseNamegives the filename without the original extension.
If you need to include subfolders too, add -Recurse to Get-ChildItem:
Get-ChildItem -Filter *.out -Recurse | Rename-Item -NewName { $_.BaseName + '.txt' }
If you need to handle specific text encodings (e.g., if some .out files have non-UTF-8 characters) or perform extra processing (like cleaning up content), Python is a great choice. Here's a simple script:
- Create a new file called
convert_out_to_txt.pyin your file folder. - Paste this code:
import os # Get all .out files in the current directory out_files = [f for f in os.listdir('.') if f.endswith('.out')] for file in out_files: # Get the filename without extension txt_filename = os.path.splitext(file)[0] + '.txt' # Read the .out file and write to .txt try: # Adjust encoding if needed (e.g., 'latin-1' for older files) with open(file, 'r', encoding='utf-8') as infile, open(txt_filename, 'w', encoding='utf-8') as outfile: content = infile.read() outfile.write(content) print(f"Converted: {file} -> {txt_filename}") except Exception as e: print(f"Error converting {file}: {str(e)}")
- Run the script by opening Cmd/PowerShell in the folder and typing
python convert_out_to_txt.py.
Note: If you get encoding errors, try changing encoding='utf-8' to encoding='latin-1' or another appropriate encoding for your files.
内容的提问来源于stack exchange,提问作者Henry Chan

