3

I have to read and write with f=open etc

When using this my file path is:

f=open("N:\gcse_computing_my_name\component_2\neatest\Login.txt")(w)

using \N or \n in this file path breaks my code. I can't change the folder name and have to use that folder.

How would I get around this?

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
Sam
  • 25
  • 7
  • 2
    Try """N:\gcse_computing_my_name\component_2\neatest\Login.txt""" which should be the raw string – pazqo Oct 04 '17 at 13:09
  • 4
    Escape it with doubleslash \\ – Anton vBR Oct 04 '17 at 13:10
  • 3
    @pazqo, eh? Triple quotes don't make something a raw string, `r'...'` syntax does. `"""foo"""` is not raw, `r"""foo"""` is. – Charles Duffy Oct 04 '17 at 13:11
  • 1
    @EricDuminil, `"""\n"""` is a string that contains one character, not two. So that's not what the OP needs. A "raw" string in Python is indeed one that doesn't have escape sequences interpreted, which (again) is what they need. – Charles Duffy Oct 04 '17 at 13:12
  • 1
    @CharlesDuffy you are completely right! I should have took 10 seconds to check in console :) – pazqo Oct 04 '17 at 13:14
  • @CharlesDuffy: Thanks for the explanation, you're right. `"""\g"""` outputs `'\\g'`, but it doesn't work with `\n`. – Eric Duminil Oct 04 '17 at 13:16
  • @EricDuminil, right -- if an escape sequence doesn't exist in Python, it just gets ignored. There is no `\g` sequence, so it's not a good one to test with. (I'm tempted to call this a design flaw in the language, but too late to fix it now). – Charles Duffy Oct 04 '17 at 13:17
  • @SamFurness, ...btw, see the related question [Why do backslashes appear twice?](https://stackoverflow.com/questions/24085680/why-do-backslashes-appear-twice), which provides more context for the behavior at hand. – Charles Duffy Oct 04 '17 at 13:20
  • @CharlesDuffy: I didn't specifically test `\g`. But by typing `"""N:\gcse_computing_my_name\component_2\neatest\Login.txt""‌​"`, I noticed that some backslashes were escaped, so I assumed they all were. – Eric Duminil Oct 04 '17 at 13:35
  • Possible duplicate of [Python escape character](https://stackoverflow.com/questions/18682695/python-escape-character) – William Oct 04 '17 at 14:27
  • @William, I really expect there to *be* a good dupe for this one somewhere, but I'm not convinced that's it. The OP there is asking how to patch up a mangled path, not how to enter it in such a way as to avoid mangling in the first place. – Charles Duffy Oct 04 '17 at 14:38

2 Answers2

8

\n is an escape sequence to Python, telling it to replace that with a single newline.

\\ is another escape sequence, which is replaced with a single backslash literal.

You also have the option to turn off interpretation of escape sequences entirely by using "raw string" syntax. Thus, your options:

  • A raw string

    f = open(r'N:\gcse_computing_my_name\component_2\neatest\Login.txt', 'w')
    
  • Doubling up your backslashes

    f = open('N:\\gcse_computing_my_name\\component_2\\neatest\\Login.txt', 'w')
    
  • Using forward slashes instead (not allowed everywhere on Windows, but perfectly acceptable in Python):

    f = open('N:/gcse_computing_my_name/component_2/neatest/Login.txt', 'w')
    
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    it is very common on windows systems to use the double backslash form for file paths. alternately, python's `open` and most `os.path` functions will automatically interpret forward slashes correctly for windows: `open("N:/gcse_computing_my_name/component_2/neatest/Login.txt")` – Aaron Oct 04 '17 at 13:23
0

I ended up using my_var.replace('\n', '') which removed the new line character and resolved the issue I was experiencing. As a result, the code is now functioning as expected.

Jay Patel
  • 545
  • 4
  • 11
iOS Newbie
  • 117
  • 1
  • 8