469,271 Members | 1,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

how to open a directory containing many files?

111 100+
i know how to open a file and access its elements..now i have to open a folder containing these files and work with these files..
the normal open("directoryname") is giving me an error.
any suggestions??
looking for ur reply!
cheers!
Dec 10 '07 #1
7 27236
ghostdog74
511 Expert 256MB
i know how to open a file and access its elements..now i have to open a folder containing these files and work with these files..
the normal open("directoryname") is giving me an error.
any suggestions??
looking for ur reply!
cheers!
check out os.listdir() or os.walk().
Dec 10 '07 #2
aboxylica
111 100+
check out os.listdir() or os.walk().
i kind of tried checking how this command really works in google i could not really understand.
what i typically want to do is th
file=open("filename","r")
instead of this i want to open the directory containing these files.
file=open("directoryname","r")
when i use
Expand|Select|Wrap|Line Numbers
  1. import os
  2. seq=os.listdir(directory name")
  3.  
doesnt seem to work
waiting for ur reply,
cheers!
Dec 10 '07 #3
ghostdog74
511 Expert 256MB
i kind of tried checking how this command really works in google i could not really understand.
what i typically want to do is th
file=open("filename","r")
instead of this i want to open the directory containing these files.
file=open("directoryname","r")
when i use
Expand|Select|Wrap|Line Numbers
  1. import os
  2. seq=os.listdir(directory name")
  3.  
doesnt seem to work
waiting for ur reply,
cheers!
what doesn't seem to work? did you print out the seq variable and see if there's any values?
Expand|Select|Wrap|Line Numbers
  1. seq = os.listdir("directory")
  2. print seq
  3.  
  4.  
Dec 10 '07 #4
bvdet
2,851 Expert Mod 2GB
i kind of tried checking how this command really works in google i could not really understand.
what i typically want to do is th
file=open("filename","r")
instead of this i want to open the directory containing these files.
file=open("directoryname","r")
when i use
Expand|Select|Wrap|Line Numbers
  1. import os
  2. seq=os.listdir(directory name")
  3.  
doesnt seem to work
waiting for ur reply,
cheers!
You cannot open a directory with the built-in function file(). You can create a filtered list of files found in a specific directory with the os module.
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. # Create a list of directory entries
  3. >>> dir_name = 'X:\\TEMP\\temp'
  4. >>> fList = os.listdir(dir_name)
  5. # The directory entries do not include the full path
  6. # Create a list of file names with full path, eliminating subdirectory entries
  7. >>> fList1 = [os.path.join(dir_name, f) for f in fList if os.path.isfile(os.path.join(dir_name, f))]
Alternatively, you can change your current working directory with os.chdir() and you will not need the full path to access the files.
Dec 10 '07 #5
aboxylica
111 100+
okay, that kind of works.that lists the file which i can handle
but to get into the files..this is what am trying
Expand|Select|Wrap|Line Numbers
  1. import os
  2. seq=os.listdir("ding")
  3. print seq
  4. # this lists all the file
  5. for i,rem in enumerate(seqq):
  6. rem=open("i","r")
  7. print rem
  8.  
  9.  
this does not seem to work.
waiting for reply,
cheers!
Dec 10 '07 #6
bvdet
2,851 Expert Mod 2GB
okay, that kind of works.that lists the file which i can handle
but to get into the files..this is what am trying
Expand|Select|Wrap|Line Numbers
  1. import os
  2. seq=os.listdir("ding")
  3. print seq
  4. # this lists all the file
  5. for i,rem in enumerate(seqq):
  6.     rem=open("i","r")
  7.     print rem
  8.  
  9.  
this does not seem to work.
waiting for reply,
cheers!
Try this (untested):
Expand|Select|Wrap|Line Numbers
  1. import os
  2. seq=os.listdir("ding")
  3. print seq
  4. os.chdir("ding")
  5. for f in seq:
  6.     if os.path.isfile(f):
  7.         rem = open("f")
  8.         print rem.read()
  9.         rem.close()
Dec 10 '07 #7
aboxylica
111 100+
thanks a lot..that helped!
Dec 10 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by KevinGravelle | last post: by
6 posts views Thread by Hemant Shah | last post: by
8 posts views Thread by Kenneth P | last post: by
11 posts views Thread by comp.lang.php | last post: by
8 posts views Thread by MoshiachNow | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.