473,406 Members | 2,549 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,406 software developers and data experts.

Get file names in a folder using Python

Hi All,

How can we get the file names in a folder using python?

Could you please help me....?

Thanks and Regards,
Shaheer
Apr 9 '07 #1
1 25197
bvdet
2,851 Expert Mod 2GB
Hi All,

How can we get the file names in a folder using python?

Could you please help me....?

Thanks and Regards,
Shaheer
We did this exercise several months ago. I just happened to have the code handy! I commented out the part you may not need.
Expand|Select|Wrap|Line Numbers
  1. import os
  2. def dir_list2(dir_name, *args):
  3.     fileList = []
  4.     for file in os.listdir(dir_name):
  5.         dirfile = os.path.join(dir_name, file)
  6.         if os.path.isfile(dirfile):
  7.             if len(args) == 0:
  8.                 fileList.append(dirfile)
  9.             else:
  10.                 if os.path.splitext(dirfile)[1][1:] in args:
  11.                     fileList.append(dirfile)
  12.         '''
  13.         elif os.path.isdir(dirfile):
  14.             print "Accessing directory:", dirfile
  15.             fileList += dir_list2(dirfile, *args)
  16.         '''
  17.     return fileList
  18.  
  19. if __name__ == '__main__':
  20.     fileList = dir_list2('path_to_directory')
  21.     for f in fileList:
  22.         print f
...and the simple way:
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> os.listdir('path to directory')
  3.  
Apr 9 '07 #2

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

Similar topics

2
by: CB Hamlyn | last post by:
Hello, I'm currently working on a Modification for the PC game Battlefield 1942. I'm a pretty good VBA Programmer with some Foxpro under belt as well. After reading a ton of stuff on-line I'm...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
2
by: gvindia | last post by:
Hai, Please help me. How can lock file or folder using c/c++ in windows. regards, gv.
6
by: sri2097 | last post by:
Hi, I'm trying to zip a particular fiolder and place the zipped folder into a target folder using python. I have used the following command in 'ubuntu'. zip_command = 'zip -qr %s %s' % (target,...
2
by: odavilar | last post by:
i'm trying to make an command shell or something like that, so i'm using system("cat archivo.txt"); for example, but what i want is todo something like system("cat"+variable), is there any way to do...
12
by: fanoftvb | last post by:
Hi, i would like to move a selected files to a folder using vb.net. I currently have this code If File.Exists("C:\Documents and Settings\055949x\transfer\text.txt") And Not...
1
by: new one | last post by:
i want to convert a html file to dom using python
2
by: Tatiana Louisa | last post by:
Hi, I am a bit new in python. Would be nice to get some help. I would like to remove the first line of 100 files in a folder. How can i proceed to write a python script. I can do it for a single...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
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...

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.