Python 写入它的文件夹路径

Python 写入它的文件夹路径,python,python-3.x,Python,Python 3.x,我有下面的代码。请帮我写下它的“输入”和“输出”文件夹路径。 例如: "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')

我有下面的代码。请帮我写下它的“输入”和“输出”文件夹路径。 例如:

"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,根据您提供的输入、问题中的预期输出和注释,我已经编写了解决您问题的代码

就我而言:

  • 输入目录:
    C:\Users\pc user\Desktop\Input

  • 输出目录:
    C:\Users\pc user\Desktop\Output

我有3个文件
A.txt
B.txt
C.txt
Input目录中,内容如下

正如你们在问题中提到的,你们必须使用你们自己的路径

»
C:\\Users\\pc user\\Desktop\\Input\\A.txt

»
C:\\Users\\pc user\\Desktop\\Input\\B.txt

»
C:\\Users\\pc user\\Desktop\\Input\\C.txt

复制以下代码,用自己的路径替换input_diroutput_dir变量的值,然后运行

»
Python代码
一旦上述代码成功执行,它将把输入目录中的所有文本文件写入输出目录。在我的例子中,Output目录中的内容如下所述:

»
C:\\Users\\pc user\\Desktop\\Output\\A.txt

»
C:\\Users\\pc user\\Desktop\\Output\\B.txt

»
C:\\Users\\pc user\\Desktop\\Output\\C.txt

@Monso,你的问题不清楚。请补充更多解释。为什么要读取trang.txt并写入trang2.txt等。提供更多输入和预期输出。您想将输入目录中的所有可用文件复制到输出目录吗?当我运行它时,trang.txt创建了trang2.txt,trang2.txt将被删除%,您能帮我为它写“输入”和“输出”路径吗?在“输入”中有多个txt文件。然后依次编辑每个文件并导出到一个新文件夹@rishikeshagrawani trang.txt中的内容是什么?为什么要将
%
替换为
'
?你写程序的初衷是什么?你为什么要删除trang2.txt?是的,我会帮助你,这就是为什么我要求输入。它工作得很好。我真的非常感谢你。你解释得很清楚。真的谢谢你没问题。祝你好运,你有空。请帮我再写一段代码。非常感谢你,当然。告诉我我能帮你什么忙吗?简要说明您的问题。Ibox facebook“Tu”ấn Hồ "
This is %nice day%.
Program %20 files % 
This is my nice journey with % Rishikesh. Monso is % also %% here.

Monso %%%% is solving the Python problems % at % mor%ning.
This is % book.
This i%s i%%s a % boy%.
I kno%w P%y%t%%ho%n. %
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);
This is nice day.
Program 20 files  
This is my nice journey with  Rishikesh. Monso is  also  here.

Monso  is solving the Python problems  at  morning.
This is  book.
This is is a  boy.
I know Python.