473,320 Members | 1,828 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.

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 7720
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: 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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.