I can't figure out how to remove the carriage return (\ n) from the file name. I create the file as follows:

# -*-coding: utf-8 -*- input_file = open("test", 'r') for line in input_file: NAMEFILE = line+".txt" print (NAMEFILE) output_file = open("./testfolder/"+NAMEFILE, 'w') output_file.write("String:"+line) output_file.close() input_file.close() 

The content of the source file is simple:

 123234 123456 .... 

Files with string names are created, that is 123234.txt 123456.txt, etc. but between the name and the extension there is a newline character, how to remove it?

    3 answers 3

    line.strip() - removes spaces and line line.strip() at the beginning and end of a line

    you can only remove line line.strip("\n") : line.strip("\n")

      Agree with vinger4:

       NAMEFILE = line.strip("\n")+".txt" 

        I think it would be better to ask why it arises.