I am writing a program where you have to extract bits of text from a text file. The specific bit of text is the save directory where I would like to save the files produced by my program. Here is my code:

    file = open("datafile.txt", "r")
    SaveDir = file.read()
    print SaveDir

That seems to be working properly, the output is exactly as it should be, "/home/marc/QuickJotProgram/QuickJotTexts "

Please note that the program skips a line before the end quote which is my problem

It's not a problem until later in the program where I use the SaveDir variable:

text_file = open(os.path.join(SaveDir, title), "w")

The error message is:

text_file = open(os.path.join(SaveDir, title), "w")
IOError: [Errno 2] No such file or directory:      '/home/marc/QuickJotProgram/QuickJotTexts\n/file'

The only thing that should not be there is the "\n" I just need to get rid of that, and the program will run smoothly.

Thanks ubuntu :)

  • 2
    sorry for posting a google search but this has been answered so many times it would be a pity to be answered again... google.com/?gws_rd=ssl#q=python%20remove%20newline
    – hytromo
    Jun 13, 2014 at 12:45
  • 2
    if you call the dupe card only duplicates on askubuntu matter @hakermania ;-)
    – Rinzwind
    Jun 13, 2014 at 12:52
  • 3
    Shouldn't this be on StackOverFlow?
    – OverCoder
    Jun 13, 2014 at 13:20
  • @Yousef's so they close it as duplicated? nah, it should just get closed.
    – Braiam
    Jun 13, 2014 at 15:14

2 Answers 2


Removing \n from a string:

newString = oldString.replace("\n", "");

Or even easier (and better):

new_str = old_str.rstrip()

This strips all whitespace on the right.


Not the answer you're looking for? Browse other questions tagged .