471,326 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

Writing a list to A CSV file (Newbie needs help)

Eclipse
G'day All

I am just starting to learn programming and need a bit of help with this one.

I want to be able to create a list containing Path, Directory, Filename and write them out to a csv file (ie path,dir,file). This is what i have so far:

import os
import csv

writer = csv.writer(open("C:\Documents and Settings\Smythville\My Documents\Python\Stuff.csv", "wb"))

for root, dirs, files in os.walk('C:\Documents and Settings\Smythville\My Documents\BitTorrent Downloads'):
writer.writerows(root, dirs, files)
f.close()

When i run it in the command window i get this:

Traceback (most recent call last):
File "C:\Documents and Settings\Smythville\My Documents\Python\Stuff\Walkdir_clean.py", line 7, in <module>
writer.writerows(root, dirs, files)
TypeError: writerows() takes exactly one argument (3 given)


I understand that I am giving it the three args but how can it convert those args into a line in a csv file.

Please help

Thanks

Pete
Dec 31 '07 #1
3 7411
bvdet
2,851 Expert Mod 2GB
G'day All

I am just starting to learn programming and need a bit of help with this one.

I want to be able to create a list containing Path, Directory, Filename and write them out to a csv file (ie path,dir,file). This is what i have so far:

import os
import csv

writer = csv.writer(open("C:\Documents and Settings\Smythville\My Documents\Python\Stuff.csv", "wb"))

for root, dirs, files in os.walk('C:\Documents and Settings\Smythville\My Documents\BitTorrent Downloads'):
writer.writerows(root, dirs, files)
f.close()

When i run it in the command window i get this:

Traceback (most recent call last):
File "C:\Documents and Settings\Smythville\My Documents\Python\Stuff\Walkdir_clean.py", line 7, in <module>
writer.writerows(root, dirs, files)
TypeError: writerows() takes exactly one argument (3 given)


I understand that I am giving it the three args but how can it convert those args into a line in a csv file.

Please help

Thanks

Pete
The csv writerows() method accepts a sequence of rows. Each row must be a sequence of strings or numbers. Perhaps it may help if you understood the information that os.walk compiles. Try the following on one of your directories:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. a = os.walk(directory_name)
  3.  
  4. for root, dir, file in a:
  5.  
  6.     print "Root directory: %s" % (root)
  7.  
  8.     if dir:
  9.         print "Subdirectories under %s:" % (root)
  10.         dirList = map(lambda x: '%s\n' % (x), dir)
  11.         dirStr = "".join(dirList)
  12.         print dirStr
  13.     else:
  14.         print "There are no subdirectories under directory %s\n" % (root)
  15.  
  16.     if file:
  17.         print "Files in directory %s:" % (root)
  18.         fileList = map(lambda x: '%s\n' % (os.path.join(root, x)), file)
  19.         fileStr = "".join(fileList)
  20.         print fileStr
  21.     else:
  22.         print "There are no files in directory %s\n" % (root)
Dec 31 '07 #2
Thanks for answering bvdet.

I used your code and played around with it to see how the os.walk function worked. If I am not mistaken the os.walk function walks down through the directories from the path supplied and creates a list as follows (Directory path,[subdir1,subdir2],[filename1,filename2]).

After looking at what os.walk returns I can see that what I was trying to do in the first post is not correct. What I was actually after is to create a csv file that shows each file from the path supplied and all subdirectories underneath it and its path. eg:

path,file1
path,file2
path\subdir,file1
path\subdir,file2
path\subdir\subsubdir1,file1
path\subdir\subsubdir2,file1

I have had a play with some more code but i don't think that this will achieve what I am after. (posted below)

When I run this code in the command window it does nothing, and i don't know why. Any Ideas???

Expand|Select|Wrap|Line Numbers
  1.  import os
  2. import csv 
  3. a = os.walk('C:\Documents and Settings\Smythville\My Documents\BitTorrent Downloads')
  4. f = open('C:\Documents and Settings\Smythville\My Documents\Python\Stuff.csv', 'w')
  5. count = 1
  6. for root, dirs, files in a:
  7. output = csv.writer(f, dialect=csv.excel, delimiter=',')
  8. output.writerows(a)
  9. print "Cycling...", count
  10. count += 1
  11. f.close()
  12.  
I put the count and print commands in to see if the code was iterating but it didn't

Thanks for your help

Eclipse
Jan 2 '08 #3
bvdet
2,851 Expert Mod 2GB
Thanks for answering bvdet.

I used your code and played around with it to see how the os.walk function worked. If I am not mistaken the os.walk function walks down through the directories from the path supplied and creates a list as follows (Directory path,[subdir1,subdir2],[filename1,filename2]).

After looking at what os.walk returns I can see that what I was trying to do in the first post is not correct. What I was actually after is to create a csv file that shows each file from the path supplied and all subdirectories underneath it and its path. eg:

path,file1
path,file2
path\subdir,file1
path\subdir,file2
path\subdir\subsubdir1,file1
path\subdir\subsubdir2,file1

I have had a play with some more code but i don't think that this will achieve what I am after. (posted below)

When I run this code in the command window it does nothing, and i don't know why. Any Ideas???

Expand|Select|Wrap|Line Numbers
  1.  import os
  2. import csv 
  3. a = os.walk('C:\Documents and Settings\Smythville\My Documents\BitTorrent Downloads')
  4. f = open('C:\Documents and Settings\Smythville\My Documents\Python\Stuff.csv', 'w')
  5. count = 1
  6. for root, dirs, files in a:
  7. output = csv.writer(f, dialect=csv.excel, delimiter=',')
  8. output.writerows(a)
  9. print "Cycling...", count
  10. count += 1
  11. f.close()
  12.  
I put the count and print commands in to see if the code was iterating but it didn't

Thanks for your help

Eclipse
Your code is not indented properly. You do not have to use the csv module. Sample code:
Expand|Select|Wrap|Line Numbers
  1. f = open('file_name', 'w')
  2. outputList = []
  3. for root, dirs, files in os.walk('dir_name'):
  4.     outputList.append(root)
  5.     for d in dirs:
  6.         outputList.append(','.join([root, d]))
  7.     for f1 in files:
  8.         outputList.append(','.join([root, f1]))
  9. f.write('\n'.join(outputList))
  10. f.close()
Jan 2 '08 #4

Post your reply

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

Similar topics

4 posts views Thread by sud jag | last post: by
3 posts views Thread by localpricemaps | last post: by
77 posts views Thread by Ville Vainio | last post: by
9 posts views Thread by jerry.upstatenyguy | last post: by

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.