Help with passing parameters

This is the place for queries that don't fit in any of the other categories.

Help with passing parameters

Postby bbesase » Wed May 08, 2013 6:16 pm

Code: Select all
path = "C:/Users/blah/Documents/Blah/ParsePractice/"
fileo = "C:/Users/blah/Documents/Blah/ParsePractice/out.txt"
info = 'kadiwekjkhafhekihwaieirwqyerywqyqrywqryiqoryeioyqwryeyeywqeejlcksahfioheo'

def getUnitTests(path):

 
    for r,d,f in os.walk(path):
        for files in f:
            #if files.endswith(".txt"):
            if '.txt' in files:
               
                print os.path.join(r,files)
               
                #return os.path.join(r,files)
               

def writeToFile(fileo):
   
   
    f = open(fileo, 'w')

    f.write(info)
    #f.write(getUnitTests(path))
    f.close()
   
getUnitTests(path)
writeToFile(fileo)


I have this code and its sloppy and ugly but I'm a beginner. I need to read through all the files in the path designated to search for all .txt files, which I can do no problem. However, in my writeToFile function i need to write every file that ends in .txt to it. I dont know how to take every file from the first function and pass it through the second function to display every filename. Some help would be fantastic. Thank you in advance.
bbesase
 
Posts: 18
Joined: Wed May 08, 2013 1:57 pm

Re: Help with passing parameters

Postby setrofim » Wed May 08, 2013 6:31 pm

Something like
Code: Select all
path = "C:/Users/blah/Documents/Blah/ParsePractice/"
fileo = "C:/Users/blah/Documents/Blah/ParsePractice/out.txt"
info = 'kadiwekjkhafhekihwaieirwqyerywqyqrywqryiqoryeioyqwryeyeywqeejlcksahfioheo'


def getUnitTests(path):
    found_files = []
    for r,d,f in os.walk(path):
        for files in f:
            #if files.endswith(".txt"):
            if '.txt' in files:
               
                path = os.path.join(r,files)
                print path
                found_files.append(path)
    return found_files
               

def writeToFile(files):
    for path in files:
        with open(path, 'w') as wfh:
            wfh.write(info)

   
found_files = getUnitTests(path)
writeToFile(found_files)
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Help with passing parameters

Postby bbesase » Wed May 08, 2013 6:46 pm

It's still not quite working, its not printing the array of file names it's still printing what I set info too. When I change it to
Code: Select all
wfh.write(found_files)




It messes up again. Any ideas why it's not printing the array of found_files out?
bbesase
 
Posts: 18
Joined: Wed May 08, 2013 1:57 pm

Re: Help with passing parameters

Postby setrofim » Wed May 08, 2013 6:50 pm

Erm, what? Do you want to write the list of all found files into each of those files? What output do you expect?
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Help with passing parameters

Postby bbesase » Wed May 08, 2013 6:53 pm

I want to write the name of each file I find from the getUnitTest function into the file I write to in the writeToFile function. So the output will be like:

blahblahblah.txt
blahblahblah1.txt
blahblahblah2.txt
blahblahblah3.txt
etc
bbesase
 
Posts: 18
Joined: Wed May 08, 2013 1:57 pm

Re: Help with passing parameters

Postby setrofim » Wed May 08, 2013 6:55 pm

I see. Then try
Code: Select all
wfh.write('\n'.join(found_files))
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Help with passing parameters

Postby bbesase » Wed May 08, 2013 6:58 pm

Awesome, it worked! Thank you so much.
bbesase
 
Posts: 18
Joined: Wed May 08, 2013 1:57 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Mekire and 3 guests