473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 27540
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

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

Similar topics

11
by: Etienne Charland | last post by:
Hi, I have a solution containing 6 C# projects; 1 WinForms project and 5 class libraries. I didn't have any problems until recently. I added a new project containing reports. I am using...
0
by: KevinGravelle | last post by:
Hello, I am running an Apache web server and my httpd configuration file is utilizing the aspdotnet_module in order to run an ASP.NET application. Whenever I try and open my existing web...
6
by: Hemant Shah | last post by:
Folks, I need to move HOME directory of an instance to another directory. What is the best way of doing it? Is changing password file enough? or dies DB2 store this info in it's own config? ...
1
by: Mink | last post by:
I have a code that checks a directory to see if it exists, and then if it does not, the code creates the directory and allows the user to add/view files in the directory. Weekly I print a report...
8
by: Kenneth P | last post by:
Hi, I've had a hd crash (with proper data backup) and I wonder what way would be the best way to set up an old project with all code from a backup hd in a new vs.net2003 environment? TIA ...
0
by: Brad Wood | last post by:
I've reinstalled my operating system and now want to reopen a web service project. Here is the URLPath in my project's .webinfo file: "http://localhost/DriverService/DriverWebService.csproj" ...
11
by: comp.lang.php | last post by:
Once again, I thought my class method deleteZip() would do the trick, but it never deletes any .zip* file found in a directory: /** * Delete any latent ZIP files found in this album. This...
8
by: MoshiachNow | last post by:
HI, I try to open a link using %windir% in path: <LI><A HREF="%windir%/temp/MainSetupLog.log">F </A></LI> The browser does not seem to find the file. However,If I paste in the Address window:...
3
by: nickvans | last post by:
Hi all, I have a database which is used to narrow down and select part drawings. The drawings are stored as .jpg files. I have a table relating the the part number to the file containing the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.