Adreamer82
Adreamer82

Reputation: 143

breaking up long path names

I am having trouble breaking the path into two lines in my python compiler. It is simply a long path on the compiler screen and i have to stretch the window too wide. I know how to break a print("string") into two lines of code that will compile correctly, but not an open(path). As I write this I notice the text box can not even hold it all on one line. print()

`raw_StringFile = open(r'C:\Users\Public\Documents\year 2013\testfiles\test          code\rawstringfiles.txt', 'a')`

Upvotes: 11

Views: 10786

Answers (4)

RyPeck
RyPeck

Reputation: 8147

Python has a nifty feature called Implicit line joining.

Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes. For example:

month_names = ['Januari', 'Februari', 'Maart',      # These are the
               'April',   'Mei',      'Juni',       # Dutch names
               'Juli',    'Augustus', 'September',  # for the months
               'Oktober', 'November', 'December']   # of the year

So for your question -

raw_StringFile = open(r'C:\Users\Public\Documents\year 2013\testfiles'
                      r'\testcode\rawstringfiles.txt', 'a')

EDIT - In this example, it is actually String literal concatenation.

Upvotes: 2

alecxe
alecxe

Reputation: 473783

You can place your string inside parenthesis, like this:

>>> (r'C:\Users\Public\Documents'
...  r'\year 2013\testfiles\test          code'
...  r'\rawstringfiles.txt')
'C:\\Users\\Public\\Documents\\year 2013\\testfiles\\test          code\\rawstringfiles.txt'

This is called "String literal concatenation". Quote from docs:

Multiple adjacent string literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation. Thus, "hello" 'world' is equivalent to "helloworld". This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings, for example:

re.compile("[A-Za-z_]"       # letter or underscore
           "[A-Za-z0-9_]*"   # letter, digit or underscore
)

Also see:

Upvotes: 4

George Mitchell
George Mitchell

Reputation: 1168

That is what the \ is for.

>>> mystr = "long" \
... "str"
>>> mystr
'longstr'

Or in your case:

longStr = r"C:\Users\Public\Documents\year 2013\testfiles" \
           r"\testcode\rawstringfiles.txt"
raw_StringFile = open(longStr, 'a')

EDIT

Well, you don't even need the \ if you use parenthesis, i.e.:

longStr = (r"C:\Users\Public\Documents\year 2013\testfiles"
               r"\testcode\rawstringfiles.txt")
raw_StringFile = open(longStr, 'a')

Upvotes: 14

ely
ely

Reputation: 77404

Python permits concatenating strings merely by placing them adjacently:

In [67]: 'abc''def'
Out[67]: 'abcdef'

In [68]: r'abc'r'def'
Out[68]: 'abcdef'

In [69]: (r'abc'
   ....: r'def')
Out[69]: 'abcdef'

So something like this ought to work for you.

raw_StringFile = open(r'C:\Users\Public\Documents\year 2013\testfiles'
                      r'\testcode\rawstringfiles.txt', 'a')

Another option is to use os.path.join:

myPath = os.path.join(r'C:\Users\Public\Documents\year 2013\testfiles',
                      r'testcode\rawstringfiles.txt')
raw_StringFile = open(myPath, 'a')

Upvotes: 7

Related Questions