Python concatenate text files

后端 未结 12 1973
無奈伤痛
無奈伤痛 2020-11-22 02:51

I have a list of 20 file names, like [\'file1.txt\', \'file2.txt\', ...]. I want to write a Python script to concatenate these files into a new file. I could op

12条回答
  •  误落风尘
    2020-11-22 03:16

      import os
      files=os.listdir()
      print(files)
      print('#',tuple(files))
      name=input('Enter the inclusive file name: ')
      exten=input('Enter the type(extension): ')
      filename=name+'.'+exten
      output_file=open(filename,'w+')
      for i in files:
        print(i)
        j=files.index(i)
        f_j=open(i,'r')
        print(f_j.read())
        for x in f_j:
          outfile.write(x)
    

提交回复
热议问题