472,956 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,956 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 7679
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

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

Similar topics

4
by: sud jag | last post by:
Hi, Iam a newbie to python. So any help on this is deeply appreciated If I append an instance of a class to a list will the list have a copy of the instance or just a reference to that...
3
by: localpricemaps | last post by:
i am having a problem writing a tuple to a text file. my code is below. what i end up getting is a text file that looks like this burger, 7up burger, 7up burger, 7up and this is instead...
77
by: Ville Vainio | last post by:
I tried to clear a list today (which I do rather rarely, considering that just doing l = works most of the time) and was shocked, SHOCKED to notice that there is no clear() method. Dicts have it,...
1
by: Strider | last post by:
Hi, A bit of a PHP newbie here who could do with some help. I have searched through many forums and resources trying to find a solution for a problem I am trying to resolve. I wish to populate...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
3
by: JJ297 | last post by:
Hello I'm a newbie to programming and need help writing an if statement. I have a database set up in SQL with the following fields: Category Questions Answers I only want one...
3
by: mintominto82 | last post by:
Hello, I am a newbie to C++. I read C++ Primer from cover to cover at least 3 times, but that did not help. I am trying to write a program that loads txt file and write the data into array. the data...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.