Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 :)

share|improve this question

closed as off-topic by Sylvain Pineau, Braiam, terdon, Eric Carvalho, Florian Diesch Jun 14 at 2:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Sylvain Pineau, Braiam, terdon, Eric Carvalho, Florian Diesch
If this question can be reworded to fit the rules in the help center, please edit the question.

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 –  hakermania Jun 13 at 12:45
2  
if you call the dupe card only duplicates on askubuntu matter @hakermania ;-) –  Rinzwind Jun 13 at 12:52
3  
Shouldn't this be on StackOverFlow? –  Yousef's Jun 13 at 13:20
    
@Yousef's so they close it as duplicated? nah, it should just get closed. –  Braiam Jun 13 at 15:14

2 Answers 2

up vote 3 down vote accepted

Removing \n from a string:

newString = oldString.replace("\n", "");
share|improve this answer

Or even easier (and better):

new_str = old_str.rstrip()

This strips all whitespace on the right.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.