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


Groups > comp.lang.python > #102147

Re: Duplicate Output

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Duplicate Output
Date 2016-01-27 10:12 +0100
Organization None
Message-ID <mailman.29.1453886008.2338.python-list@python.org> (permalink)
References <56A7BF9A.1070709@verizon.net>

Show all headers | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web