473,320 Members | 1,876 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,320 software developers and data experts.

How to open and read through multiple files in a directory with python

11
I can open and read the content of a single text file using the following line of code.

Expand|Select|Wrap|Line Numbers
  1. f = open('C:\\xyz.text', 'r')
  2.      f.read()
How can I read through multiple files in a directory? as I would like to compare them based on the similarity of their content (textual content). Any suggests? thanks.
Jun 27 '13 #1

✓ answered by bvdet

You can use os.walk to compile a list of file names in a directory. Try the following to see what information os.walk provides:
Expand|Select|Wrap|Line Numbers
  1. for root, dirs, files in os.walk(dir_name):
  2.     print root
  3.     print "  "+"\n  ".join(dirs)
  4.     print "    "+"\n    ".join(files)
  5.     print "*"*40
  6.  

4 6850
bvdet
2,851 Expert Mod 2GB
You can use os.walk to compile a list of file names in a directory. Try the following to see what information os.walk provides:
Expand|Select|Wrap|Line Numbers
  1. for root, dirs, files in os.walk(dir_name):
  2.     print root
  3.     print "  "+"\n  ".join(dirs)
  4.     print "    "+"\n    ".join(files)
  5.     print "*"*40
  6.  
Jun 27 '13 #2
Tiger1
11
Hi bvdet, thanks for the response. The code worked, but the content of the files were not displayed, only the file names. Is there a way display the contents of the files in the directory? as another module would want to read the content of the files and compare them.
Jun 27 '13 #3
bvdet
2,851 Expert Mod 2GB
To open a file from os.walk output, you need to concatenate the root directory and file name:
Expand|Select|Wrap|Line Numbers
  1. f1 = os.path.join(root, file_name)
If you want to see if the contents of two files are equal:
Expand|Select|Wrap|Line Numbers
  1. open(f1).read() == open(f2).read()
Jun 27 '13 #4
Tiger1
11
Hi bvdet, thanks for help. You its now sorted.
Jun 30 '13 #5

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

Similar topics

3
by: Kay Lee | last post by:
Hi, I looked up os module to find out some method to move and copy files in python, but os doesn't support such methods. Is there any way to move & copy files in python? Thanks in adv.
4
by: googlinggoogler | last post by:
Hiya, The title says it all really, but im a newbie to python sort of. I can read in files and write files no probs. But what I want to do is read in a couple of files and output them to one...
5
by: JenHu | last post by:
Hi experts, I wrote a function which retrieves a file in the folder, the file path is : Dim sr As New StreamReader(strFilepath & ReturnFileName) What if I have more than 1 file_name in...
6
by: Anand | last post by:
Hello, Can I get some help on how to read the excel files using python? from win32com.client import Dispatch xlApp = Dispatch("Excel.Application") xlWb = xlApp.Workbooks.Open("Read.xls")...
15
by: leorulez | last post by:
Is there any way to read multiple files (more than 1000 files) and then write into one single output file using C? Right now in my program, I have a loop which asks for the filename and writes into...
3
by: siddhi | last post by:
Hi, I am very very new to vb6. I am trying to browse and read multiple microsoft word files. i am able to do for one file as follow, but not multiple files. Please I need help ASAP. Thanks Private...
0
by: kdsutaia | last post by:
hi! I hv to read two files from two diff directories and need to count pairs of words from both the files. as well I hv to keep track of no of documents where this terms present. All files...
2
by: jai_python | last post by:
hi frenz I Need a Python Script For read multiple files(.txt) from a folder and write it in a single text file.... Thanks
0
by: Tim Chase | last post by:
new_file = open('filename.txt', 'w') import os for fname in os.listdir('.'): if not fname.lower().endswith('.txt'): continue new_file_name = '%s_parsed%s' % tuple( new_file =...
1
by: Sumit jain | last post by:
#Looking for logic which can modify multiple files in a #directory.Eg /Sumit/Files/ in this there are 3 file #Sam123.12,Sam437.34,Sam676.81.read file one by one and #need only those line which can...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.