Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102147 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2016-01-27 10:12 +0100 |
| Last post | 2016-01-27 10:12 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Duplicate Output Peter Otten <__peter__@web.de> - 2016-01-27 10:12 +0100
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-01-27 10:12 +0100 |
| Subject | Re: Duplicate Output |
| Message-ID | <mailman.29.1453886008.2338.python-list@python.org> |
Gary Roach wrote:
> Hi
>
> Debian stretch OS
> KDE Desktop
> Code written with Kate
> Run in command line with $python getFileNames.py
>
> Code:
>
> from os import walk
> import subprocess
>
> f = []
> x = ""
> for (dirpath, dirnames, filenames) in walk('.'):
> print(filenames)
>
> This prints [<file list>][<duplicate file list>]
>
> What am I doing wrong or how do I remove the duplicate list.
If there is a second list that list is for a subdirectory of ".". To see
that add
> for (dirpath, dirnames, filenames) in walk('.'):
print(dirpath)
> print(filenames)
to your code. If you only want the names of files in the current directory
you can break out of the loop after the first iteration
> for (dirpath, dirnames, filenames) in walk('.'):
> print(filenames)
break
or use next():
filenames = next(walk("."))[2]
print(filenames)
Back to top | Article view | comp.lang.python
csiph-web