Python - long string on multiple lines

Is there a proper way to show file paths (hard-coded) longer than 79 characters (based on pep8) for line multiplication or is it better to save the file path on one line?

For example,

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

Will the above example work best on multiple lines or on one line?

+7
python file directory
source share
2 answers

I personally use this method and saw how it was used in 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 code width up to 79 characters, and usually docs and comments up to 72.

I also recommend os.path look at os.path .

+11
source share

It is probably best not to have hard-coded file paths. Consider using relative paths or another more robust method. Unless you just make a quick script to run only on your computer, in which case it doesn’t matter what PEP8 wants you to do.

To answer the question, you can 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") 
-3
source share

All Articles