Prev: extra rows in a CSV module output when viewed in excel 2007
Next: writing \feff at the begining of a file
From: blur959 on 13 Aug 2010 03:54 Hi, all, Is there a way to get a number of files in a particular directory? I tried using os.walk, os.listdir but they are return me with a list, tuple of the files, etc. But I want it to return a number. Is it possible?
From: Bruno Desthuilliers on 13 Aug 2010 04:44 blur959 a �crit : > Hi, all, Is there a way to get a number of files in a particular > directory? I tried using os.walk, os.listdir but they are return me > with a list, tuple of the files, etc. But I want it to return a > number. Is it possible? len(any_sequence)
From: Cameron Simpson on 13 Aug 2010 04:51 On 13Aug2010 00:54, blur959 <blur959(a)hotmail.com> wrote: | Hi, all, Is there a way to get a number of files in a particular | directory? I tried using os.walk, os.listdir but they are return me | with a list, tuple of the files, etc. But I want it to return a | number. Is it possible? Measure the length of the list returned to you? The len() built in function suggests itself... -- Cameron Simpson <cs(a)zip.com.au> DoD#743 http://www.cskk.ezoshosting.com/cs/
From: blur959 on 13 Aug 2010 05:18 Hi, I tried that, but it doesn't seem to work. My file directory has many different files extensions, and I want it to return me a number based on the number of files with similar files extensions. But when I tried running it, I get many weird numbers. Below is my code i had so far and the result. import os directory = raw_input("Please input file directory. \n\n") s = raw_input("Please input a name to replace. \n\n") ext = raw_input("input file ext") for files in os.listdir(directory): if ext in files: file_number = len(files) print file_number The result is: 13 13 13 6 15 8 10 10 8 7 5 where the result should be just 11. Can anyone help me? Thanks.
From: Stefan Schwarzer on 13 Aug 2010 05:30 On 2010-08-13 11:18, blur959 wrote: > import os > > directory = raw_input("Please input file directory. \n\n") > s = raw_input("Please input a name to replace. \n\n") > ext = raw_input("input file ext") > > for files in os.listdir(directory): > if ext in files: > file_number = len(files) > print file_number > > > The result is: > 13 > 13 > 13 > 6 > 15 > 8 > 10 > 10 > 8 > 7 > 5 > > where the result should be just 11. Can anyone help me? Thanks. `os.listdir` returns a list of names. What you named `files` should actually be `filename`. What you got printed in the loop are the lengths of each filename. Stefan
|
Next
|
Last
Pages: 1 2 3 Prev: extra rows in a CSV module output when viewed in excel 2007 Next: writing \feff at the begining of a file |