2 Replies - 808 Views - Last Post: 03 March 2015 - 06:17 PM Rate Topic: -----

#1 pcboyyyy   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 29-August 10

Traversing, opening, reading and writing files

Posted 02 March 2015 - 07:31 PM

So, I'm trying to figure out how to open multiple files in multiple directories. Read the contents of those files, write specific content to a file and use that info to create an XML, which can then be imported into a SQL database.

I have this so far:

import os
 
rootDir = '/Users/myfiles/documents'
for dirName, subdirList, fileList in os.walk(rootDir):
    print('Found directory: %s' % dirName)
    # Remove the first entry in the list of sub-directories
    # if there are any sub-directories present
    if len(subdirList) > 0:
        del subdirList[0]
    for fname in fileList:
        print('\t%s' % fname)



I am skipping the first directory, because I don't need the info from within.

How do I open, read and write the information from the files in the other directories to a file?

Is This A Good Question/Topic? 0
  • +

Replies To: Traversing, opening, reading and writing files

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,256
  • Joined: 12-December 12

Re: Traversing, opening, reading and writing files

Posted 03 March 2015 - 05:50 AM

Quote

How do I open, read and write the information from the files in the other directories to a file?

This is too broad a request for a forum question. You need to break it down into steps, make some progress, and then come back if you have a specific question.

You might start here:

Reading and Writing Files :the docs

You cannot directly edit a file. You'll need to read the file's content, construct a new version of the file (with the additional content you want to add) and save this new version.
Was This Post Helpful? 0
  • +
  • -

#3 pcboyyyy   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 29-August 10

Re: Traversing, opening, reading and writing files

Posted 03 March 2015 - 06:17 PM

I did read this and it didn't help me:

Reading and Writing Files :the docs

I don't want to edit a file. For now in the process, I want to open, read and close each file in a specific directory, reading them line by line.

Once I can accomplish that task, I want to search for specific content in each file.

Once I accomplish that, I want to use the specific searched content to create an XML file.

Once I accomplish that, I want to import the XML file into an SQL database.

Is it more clear now?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1