This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.listdir behaviour
Type: behavior Stage:
Components: None Versions: Python 2.6
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: bigaddo, mark.dickinson
Priority: normal Keywords:

Created on 2009-09-29 07:07 by bigaddo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg93255 - (view) Author: Chris Adamson (bigaddo) Date: 2009-09-29 07:07
When I iterate through a list created using os.listdir it seems to grow
as I create files in that directory. I want a static copy of the list of
files in the directory prior to me writing new files into it.

Here is my code:

fileList = os.listdir(temporaryDirectory)

for curFile in fileList:
   # print the file list to see if it is indeed growing
   print FileList
   fp = file(os.path.join(temporaryDirectory, "." + curFile), 'w')
   # write stuff
   fp.close()

Here is the output:

['a', 'b', 'c']
['a', 'b', 'c', '.a']
['a', 'b', 'c', '.a', '.b']
['a', 'b', 'c', '.a', '.b', '.c']

So the list is growing and eventually curFile iterates through the list
of files that were created. This behaviour was unwanted in my case and I
couldn't find any documentation about this.
msg93266 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-09-29 09:44
What's 'FileList' in your code?  (It's not the same as 'fileList', since
Python identifiers are case sensitive.)

What's 'temporaryDirectory'?

Please could you cut and paste an executable code snippet that exhibits
the problem, along with any other instructions necessary to reproduce
(e.g. "First create a directory 'tmp' containing files 'a', 'b' and 'c'").
msg93340 - (view) Author: Chris Adamson (bigaddo) Date: 2009-09-30 00:14
I think I found my problem. Since I didn't *COPY* the array that I
created with os.listdir I was actually appending to it, my bad.
History
Date User Action Args
2022-04-11 14:56:53adminsetgithub: 51266
2009-09-30 00:14:07bigaddosetstatus: pending -> closed

messages: + msg93340
2009-09-29 09:44:12mark.dickinsonsetstatus: open -> pending

nosy: + mark.dickinson
messages: + msg93266

resolution: works for me
2009-09-29 07:07:29bigaddocreate