473,706 Members | 5,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to open a directory containing many files?

111 New Member
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("directory name") is giving me an error.
any suggestions??
looking for ur reply!
cheers!
Dec 10 '07 #1
7 27559
ghostdog74
511 Recognized Expert Contributor
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("directory name") 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 New Member
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("file name","r")
instead of this i want to open the directory containing these files.
file=open("dire ctoryname","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 Recognized Expert Contributor
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("file name","r")
instead of this i want to open the directory containing these files.
file=open("dire ctoryname","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 Recognized Expert Moderator Specialist
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("file name","r")
instead of this i want to open the directory containing these files.
file=open("dire ctoryname","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 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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
1897
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 ActiveReports.Net. Now, whenever I make a change to a report in the class library and recompile, I get this error: "Could not copy temporary files to the output directory.". I have to close VS.Net and reopen it in order to recompile, this is very cumbersome....
0
2378
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 project, I get the error message: "Unable to get the project file from the Web server. Unable to open project 'WebApplication1'. The file path 'C:\Program Files\Apache Group\Apache2\htdocs\WebApplication1' does not correspond to the URL
6
8916
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? I am running UDB 8.2 on Linux and AIX. Thanks.
1
1479
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 containing a copy of each record entered during that week (we are still no paperless here) and I have to manually select the files and print them as well. I have seen a code for Access to pick all the files in a directory, but after that is there...
8
1692
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 Kenneth P
0
1037
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" So I create a new virtual directory in IIS, name it "DriverService", and set it's local path to the directory containing my .csproj (and other project files). I verified that IIS is working by creating a simple page in wwwroot and calling...
11
5802
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 method is to be inherited by all listing classes to allow for * list-wide deletion of latent server-created ZIP files for security purposes *
8
6137
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: file:///%windir%/temp/MainSetupLog.log
3
6582
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 drawing: PartNum / file.extension. All files are stored in a set directory. I'm trying to allow some functionality in my form to allow the user to click a button or select a file from a list and have that file open inside windows picture viewer. I...
0
8778
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8693
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9150
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7899
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4707
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2087
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.