8

Is there a correct way to show file paths (hard coded) longer than 79 characters (based on pep8) on multiply lines or is it best to keep the file path on a single line?

Eg

photos = "D:\MyWork\FieldWork\Year2015\January\MountainPass\Area1\Site1\Campsite2\Inspections\photos1"

Would the above example work best on multiple lines or a single line?

2 Answers 2

14

I personally use this method, and have seen it used in the PEP8 materials:

long_string = ('this is a really long string I want '
               'to wrap over multiple lines')

You can also do:

long_string = 'this is a really long string I want '\
              'to wrap over multiple lines'

According to PEP8 you should try to keep the maximum width of code to 79 characters, and generally docstrings and comments to 72.

I also recommend taking a look at os.path.

Sign up to request clarification or add additional context in comments.

8 Comments

For those wondering, PEP recommends this character limit because it is generally the width of a half-window; enabling programmers to open multiple, side-by-side windows.
@ZachGates: What's a half-window? Terminal windows are usually 80x25 (or 80x24 or 80x50 or similar), and many other tools (like SO) default to 80-character width because it's such an old and prevalent standard for source code; I'm pretty sure it's fitting in that whole window that's the reasoning behind PEP 8. And I can fit about 3.2 of terminal windows in the default font on my (modern wide-screen) monitor screen at default resolution and font size.
A half-window being a half-screen on the average displays (1024×768 and 1366×768). @abarnert
@ZachGates: Who still uses 1024x768, or anything else at 1.33x? I can't remember the last time I saw a monitor that wasn't "wide screen" Also, a default Terminal.app window on a Mac is 533 pixels, and a cmd.exe on Windows looks slightly bigger, so you can't actually fit two of them on a 1024x768 screen without overlapping or going past the edge of the monitor. And again, I'm pretty sure the reason for PEP8 picking the 80-column standard is because it was already a prevalent standard long before Python or 1024x768 monitors, from even before the days of 80-column teletypes and VDUs.
@ZachGates: … Code-review tools, etc. use fonts that fit two 80-column source files on the screen because of that ancient rule, not the other way around.
|
-6

It's probably best to not have hard-coded file paths at all. Consider using relative paths or some other more robust method. Unless you are just making a quick script to run on your computer alone, in which case it doesn't matter so much about what PEP8 wants you to do.

To actually answer the question, you could do this:

photos = "D:\MyWork\FieldWork\Year2015\January\MountainPass\\"+\
         "Area1\Site1\Campsite2\Inspections\photos1"

Or

photos = ("D:\MyWork\FieldWork\Year2015\January\MountainPass\\",
     "Area1\Site1\Campsite2\Inspections\photos1")

1 Comment

Adding the + is unnecessary; it will either do nothing, or slow things down slightly. Adding the comma is not just unnecessary, but actually breaks the code; it gives you a tuple of two strings, instead of a single string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.