129

I am trying to print out Python path folders using this:

import sys
print sys.path

The output is like this:

>>> print sys.path
['.', '/usr/bin', '/home/student/Desktop', '/home/student/my_modules', '/usr/lib/pyth
on2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/pyth
on2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-pack
ages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/
usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/
python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/p
ython2.6/dist-packages/wx-2.8-gtk2-unicode']

How do I print them into separate lines so I can parse them properly?

It should be like this:

/usr/bin
/home/student/Desktop
/home/student/my_modules
etc
khelwood
  • 55,782
  • 14
  • 81
  • 108
Larry
  • 1,319
  • 2
  • 9
  • 5

10 Answers10

227
print("\n".join(sys.path))

(The outer parentheses are included for Python 3 compatibility and are usually omitted in Python 2.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
92

Use the print function (Python 3.x) or import it (Python 2.6+):

from __future__ import print_function

print(*sys.path, sep='\n')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JBernardo
  • 32,262
  • 10
  • 90
  • 115
29

Use the splat operator (*).

By default, print prints arguments separated by space. Use sep argument to specify the delimiter:

print(*sys.path, sep="\n")
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
gautam
  • 398
  • 3
  • 6
  • 2
    **This is the new and smart way to do it!** Much better than using the confusing and verbose *lambda*s or *map/joins*. The "splat" is explained better [here](https://medium.com/understand-the-python/understanding-the-asterisk-of-python-8b9daaa4a558). – not2qubit Oct 21 '20 at 17:24
  • 5
    How is that different from [this](https://stackoverflow.com/a/6168360/6045800) answer from 2011? – Tomerikoo Mar 16 '21 at 12:24
23

Another good option for handling this kind of option is the pprint module, which (among other things) pretty prints long lists with one element per line:

>>> import sys
>>> import pprint
>>> pprint.pprint(sys.path)
['',
 '/usr/lib/python27.zip',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7/site-packages',
 '/usr/lib/python2.7/site-packages/PIL',
 '/usr/lib/python2.7/site-packages/gst-0.10',
 '/usr/lib/python2.7/site-packages/gtk-2.0',
 '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info',
 '/usr/lib/python2.7/site-packages/webkit-1.0']
>>> 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • 4
    if `a = [1,3,5]`, then that line will only print this array in one line... is there a guaranteed way for `pprint` to print it on separate lines? – nonopolarity Feb 14 '16 at 04:43
  • 1
    @太極者無極而生 `pprint.pprint(x,width=max([len(str(s)) for s in x])+3)` – ttb May 01 '18 at 14:51
19
for path in sys.path:
    print(path)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
  • Caution: do not ever try this with a notebook controlled by `papermill`! Extremely inefficient approach, and if you overuse it (e.g. put it in hundreds of cells), `papermill` will process the notebook about 10 times slower than without all these loops. You may also get `Timeout waiting for IOPub output` and `papermill: Autosave too slow` errors. – mirekphd Feb 25 '21 at 21:52
  • @mirekphd, do you know what about this breaks papermill? – Winston Ewert Feb 26 '21 at 22:04
  • I can only speculate as for the reasons... the things I do know is that this used to work fine before January 10 and that downgrading all libraries (whole containers) or changing the browser or switching to Lab does not help. What did help was switching to a one-liner based on `join` (it restored `papermill` speeds equivalent to no printing). – mirekphd Feb 27 '21 at 10:32
9

Sven Marnach's answer is pretty much it, but has one generality issue... It will fail if the list being printed doesn't just contain strings.

So, the more general answer to "How to print out a list with elements separated by newlines"...

print '\n'.join([ str(myelement) for myelement in mylist ])

Then again, the print function approach JBernardo points out is superior. If you can, using the print function instead of the print statement is almost always a good idea.

travc
  • 1,788
  • 1
  • 18
  • 9
  • 1
    You should avoid using list comprehension where a generator expression can be used, and you can definitely use one here. Take a look at [this answer](http://stackoverflow.com/a/35837818/3375713). You will get more useful links in that answer, like [this one](http://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehension) – Sнаđошƒаӽ Apr 08 '16 at 03:57
  • 2
    @Sнаđошƒаӽ not true in this case, see https://stackoverflow.com/a/37782238/5320906 – snakecharmerb Oct 11 '20 at 20:04
  • @snakecharmerb it's good to know. Thanks a lot for sharing! – Sнаđошƒаӽ Oct 12 '20 at 00:27
3

A slightly more general solution based on join, that works even for pandas.Timestamp:

print("\n".join(map(str, my_list)))

mirekphd
  • 4,799
  • 3
  • 38
  • 59
2

For printing list elements on separate lines, you can use:

files = ['test1.txt', 'test2.txt', 'test3.txt']
for i in range(len(files)): print(files[i])
Cyborg
  • 1,437
  • 19
  • 40
1

sys.path returns the list of paths

ref

sys.path

A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

import sys
dirs=sys.path
for path in dirs:
   print(path)

or you can print only first path by

print(dir[0])

0

You can also turn your list into a numpy array of size len(sys.path)

print(np.array(sys.path).reshape(-1,1))

outputs:

[['.']
 ['/usr/bin']
 ['/home/student/Desktop']
 ['/home/student/my_modules']
 ['/usr/lib/python2.6']
 ['/usr/lib/python2.6/plat-linux2']
 ['/usr/lib/python2.6/lib-tk']
 ['/usr/lib/pyton2.6/lib-old']
 ['/usr/lib/python2.6/lib-dynload']
 ['/usr/local/lib/python2.6/dist-packages']
 ['/usr/lib/python2.6/dist-packages']
 ['/usr/lib/python2.6/dist-packages/PIL']
 ['/usr/lib/python2.6/dist-packages/gst-0.10']
 ['/usr/lib/pymodules/python2.6']
 ['/usr/lib/python2.6/dist-packages/gtk-2.0']
 ['/usr/lib/pymodules/python2.6/gtk-2.0']
 ['/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode']]
braulio
  • 543
  • 2
  • 13