我有以下代码。请帮我写一下“输入”和“输出”文件夹路径。 例如:
"input": C: \ Users \ Name \ Desktop \ Input \ *. Txt
"output": C: \ Users \ Name \ Desktop \ Output \ * .txt (it creates new txt)
输入中的txt文件名保存输出。
从许多txt文件的文件夹中删除“%”并将其导出到另一个文件夹
f1 = open('trang.txt', 'r')
f2 = open('trang2.txt', 'w')
for line in f1:
f2.write(line.replace('%', ''))
f1.close()
f2.close()
我使用的是python 3.6.2
答案 0 :(得分:0)
@Monso ,根据您提供的输入,问题和预期输出在评论中,我写了代码来解决你的问题。
就我而言:
输入目录:result_list = sorted(chain(job, apartments),
key=lambda instance: instance.date)
输出目录:C:\Users\pc-user\Desktop\Input
我有3个文件C:\Users\pc-user\Desktop\Output
,A.txt
& 输入目录中的B.txt
,内容如下。
你必须像在问题中提到的那样使用你自己的路径。
C.txt
C:\\Users\\pc-user\\Desktop\\Input\\A.txt
This is %nice day%.
Program %20 files %
C:\\Users\\pc-user\\Desktop\\Input\\B.txt
This is my nice journey with % Rishikesh. Monso is % also %% here.
Monso %%%% is solving the Python problems % at % mor%ning.
C:\\Users\\pc-user\\Desktop\\Input\\C.txt
复制以下代码,将 input_dir 和 output_dir 变量的值替换为您自己的路径并运行。
This is % book.
This i%s i%%s a % boy%.
I kno%w P%y%t%%ho%n. %
Python code
上述代码成功执行后,会将 input_dir 中的所有文本文件写入 output_dir 。就我而言,输出目录中的内容如下所示:
import glob
import os
def copy_files_to_output(input_dir, output_dir):
# Creating a regex path for all text files inside Input directory
input_dir_regex_path = os.path.join(input_dir, "*.txt");
# Getting list of absolute paths of all the text files inside Input directory
input_files_paths = glob.glob(input_dir_regex_path);
# Iterating over a list of input file names
for full_path in input_files_paths:
# Get text file name from absolute path
file_name = full_path.split('\\')[-1]
# Reading input text file
# (Open file in read mode, 'r' is default here; the 2nd default parameter to open())
# You do not need to close file (if you open as follows using with statement)
with open(full_path) as in_file:
# Creating full path name of output text file
output_file_path = output_dir + "\\" + file_name;
# Create a new file inside Output directory
# 'w' specifies to open file in write mode
with open(output_file_path, 'w') as out_file:
# Get list of lines in text file
all_lines = in_file.readlines();
# Write all lines to a new file (inside Output directory)
for line in all_lines:
# Replace all '%' characters present in line with ''
line = line.replace('%', '');
out_file.write(line);
# Starting point
if __name__ == "__main__":
try:
# Code to be executed
input_dir = "C:\\Users\\pc-user\\Desktop\\Input"; # Use your own path
output_dir = "C:\\Users\\pc-user\\Desktop\\Output"; # Use your own path
# Call copy_files_to_output() function
copy_files_to_output(input_dir, output_dir);
# Operation successful
print("Copy operation successful");
except Exception as error:
# Code to be executed if any Exception occurs
print("Exception occurred", error);
C:\\Users\\pc-user\\Desktop\\Output\\A.txt
This is nice day.
Program 20 files
C:\\Users\\pc-user\\Desktop\\Output\\B.txt
This is my nice journey with Rishikesh. Monso is also here.
Monso is solving the Python problems at morning.
C:\\Users\\pc-user\\Desktop\\Output\\C.txt