Splitting a long path name

I had a problem splitting a path into two lines in my python compiler. This is just a long way on the compiler screen, and I need to stretch the window too wide. I know how to split print ("line") into two lines of code that will compile correctly but not open (path). When I write this, I noticed that the text field cannot even keep everything on one line. Print ()

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

Used for this \.

>>> 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 \it if you use parentheses, i.e.:

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

Python :

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'

- .

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

- os.path.join:

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

, :

>>> (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'

"String literal concatenation". docs:

( ), , , . , "" "" "helloworld". , , :

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

:

+2

Python , , .

, . :

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

, -

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

EDIT. .

+1

Python 3.5 Windows 7, :

imgPath = ("D:\EclipseNEON\EclipseWorkspaces\EclipsePythonWorkspace"
           "\PythonLessons\IntroducingPython\GUIs\OReillyTarsierLogo.png")

The key point is that at least on Windows, the last character in a given substring CANNOT be a backslash character ("\").

-one
source

All Articles