@Monso, as per your provided inputs, expected outputs in problem & in comments, I’ve written the code to solve your problem.
In my case:
-
Input directory:
C:\Users\pc-user\Desktop\Input
-
Output directory:
C:\Users\pc-user\Desktop\Output
And I have 3 file A.txt
, B.txt
& C.txt
inside Input directory with contents as follows.
You’ve to use your own paths as you’ve mentioned in problem.
» 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
This is % book.
This i%s i%%s a % boy%.
I kno%w P%y%t%%ho%n. %
Copy the below code, replace the value of input_dir and output_dir variables with your own paths and run.
» Python code
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);
Once the above code executes successfully, it will write all text files present inside input_dir to output_dir. In my case, the contents inside Output directory looks like as mentioned below:
» 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
This is book.
This is is a boy.
I know Python.
6
solved write the folder path for it [closed]