Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102147 > unrolled thread

Re: Duplicate Output

Started byPeter Otten <__peter__@web.de>
First post2016-01-27 10:12 +0100
Last post2016-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.


Contents

  Re: Duplicate Output Peter Otten <__peter__@web.de> - 2016-01-27 10:12 +0100

#102147 — Re: Duplicate Output

FromPeter Otten <__peter__@web.de>
Date2016-01-27 10:12 +0100
SubjectRe: 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)

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web