How do I get number of files in a particular directory.

B

blur959

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?
 
B

Bruno Desthuilliers

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)
 
C

Cameron Simpson

| 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...
 
B

blur959

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.
 
S

Stefan Schwarzer

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
 
P

Peter Otten

blur959 said:
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.

Use glob.glob() instead of os.listdir() if you are only interested in files
with a specific extension:
42

Peter
 
B

blur959

Use glob.glob() instead of os.listdir() if you are only interested in files
with a specific extension:


42

Peter


Hi, I want to make it such that the user inputs a file extension and
it prints the number of similar file extensions out.
I tried doing this:

directory = raw_input("input file directory")
ext = raw_input("input file ext")

file_list = len(glob.glob(ext))
print file_list


And my result was this:
0
which it is suppose to be 11

May I know why? And how do I specify which directory it is searching
the files extension from? I want the user to specify a directory and
it searches the particular directory for the particular file
extensions and prints the number out.
Hope you guys could help.

Thanks
 
P

Peter Otten

blur959 said:
Hi, I want to make it such that the user inputs a file extension and
it prints the number of similar file extensions out.
I tried doing this:

directory = raw_input("input file directory")
ext = raw_input("input file ext")

file_list = len(glob.glob(ext))
print file_list


And my result was this:
0
which it is suppose to be 11

May I know why? And how do I specify which directory it is searching
the files extension from? I want the user to specify a directory and
it searches the particular directory for the particular file
extensions and prints the number out.
Hope you guys could help.

The part of the filename you don't care about has to be replaced with a "*":

import os
import glob
directory = raw_input("directory? ")
ext = raw_input("file extension? ")
pattern = os.path.join(directory, "*" + ext)
matching_files = glob.glob(pattern)
print len(matching_files), "matching files"

Note that you'll learn a lot more when you look up in the documentation how
the functions mentioned actually work instead of coming back here for every
tiny detail. I you don't feel ready yet to understand enough of the Python
documentation, go one step back and work your way through a tutorial or
introductory book. A starting point is here:

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Peter
 
D

Dave Angel

blur959 said:
Hi, I want to make it such that the user inputs a file extension and
it prints the number of similar file extensions out.
I tried doing this:

directory =aw_input("input file directory")
ext =aw_input("input file ext")

file_list =en(glob.glob(ext))
print file_list


And my result was this:
0
which it is suppose to be 11

May I know why? And how do I specify which directory it is searching
the files extension from? I want the user to specify a directory and
it searches the particular directory for the particular file
extensions and prints the number out.
Hope you guys could help.

Thanks
Glob doesn't do much useful unless there's a wildcard. And as you point
out, it also needs the directory. You want to pass it something like
mydirect/*.txt

In your case, you might construct that with something like
os.path.join(directory, "*." + ext)

DaveA
 
B

blur959

Hi all, I got a problem with my script. Everything looks good so far
but for some reason my os.rename isn't working. Can anyone tell me
why? Hope you guys could help. Thanks.




import os
import glob
directory = raw_input("directory? ")
ext = raw_input("file extension? ")
r = raw_input("replace name")
pattern = os.path.join(directory, "*" + ext)
matching_files = glob.glob(pattern)
file_number = len(matching_files)



for filename in os.listdir(directory):
if ext in filename:
path = os.path.join(directory, filename)
seperated_names = os.path.splitext(filename)[0]
replace_name = filename.replace(seperated_names, r)
split_new_names = os.path.splitext(replace_name)[0]

for pad_number in range(0, file_number):
padded_numbers = "%04d" % pad_number
padded_names = "%s_%s" % (split_new_names, padded_numbers)
newpath = os.path.join(directory, padded_names)
newpathext = "%s%s" % (newpath, ext)


new_name = os.rename(path, newpathext)
 
A

Alister Ware

Hi all, I got a problem with my script. Everything looks good so far but
for some reason my os.rename isn't working. Can anyone tell me why? Hope
you guys could help. Thanks.
<snip>

You have a number of logic flaws in your code.
1st you do not actually need to know how many matching files you have.
2nd you need to rename the file inside the same loop that is splitting
your file name (this is why you are failing, your rename loop has the
same filename each time around),

I knocked up a quick hack loosely based on your original code that will
generate the file names for you. it has some stratigic print statements
so you can see what is happening (always a good idea when debugging code)

I have left the actual re-naming of the files for you to complete,
I would also agree with the earlier suggestion that you work through some
on line tutorials

import os
import glob
def rename(filelist): #function to add a count to each filenale in a list
count=0
for fullname in filelist:
count+=1
print "full file path %s" % fullname
path,filename=os.path.split(fullname)
name,ext=os.path.splitext(filename)
print "path: '%s' Name: '%s' Extn: '%s'" % (path,name,ext)
newname="%s_%04d.%s" %(name,count,ext)
print "New filename: '%s'" % newname
# rename filename to newname goes here
# dont forget you also need path

directory = raw_input("directory? ")
ext = raw_input("file extension? ")
r = raw_input("replace name")
pattern = os.path.join(directory, "*." + ext)
matching_files = glob.glob(pattern)
rename(matching_files)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top