Duplicate Output

Peter Otten __peter__ at web.de
Wed Jan 27 04:12:53 EST 2016


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)




More information about the Python-list mailing list