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
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: - import os
-
a = os.walk(directory_name)
-
-
for root, dir, file in a:
-
-
print "Root directory: %s" % (root)
-
-
if dir:
-
print "Subdirectories under %s:" % (root)
-
dirList = map(lambda x: '%s\n' % (x), dir)
-
dirStr = "".join(dirList)
-
print dirStr
-
else:
-
print "There are no subdirectories under directory %s\n" % (root)
-
-
if file:
-
print "Files in directory %s:" % (root)
-
fileList = map(lambda x: '%s\n' % (os.path.join(root, x)), file)
-
fileStr = "".join(fileList)
-
print fileStr
-
else:
-
print "There are no files in directory %s\n" % (root)
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??? - import os
-
import csv
-
a = os.walk('C:\Documents and Settings\Smythville\My Documents\BitTorrent Downloads')
-
f = open('C:\Documents and Settings\Smythville\My Documents\Python\Stuff.csv', 'w')
-
count = 1
-
for root, dirs, files in a:
-
output = csv.writer(f, dialect=csv.excel, delimiter=',')
-
output.writerows(a)
-
print "Cycling...", count
-
count += 1
-
f.close()
-
I put the count and print commands in to see if the code was iterating but it didn't
Thanks for your help
Eclipse
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??? - import os
-
import csv
-
a = os.walk('C:\Documents and Settings\Smythville\My Documents\BitTorrent Downloads')
-
f = open('C:\Documents and Settings\Smythville\My Documents\Python\Stuff.csv', 'w')
-
count = 1
-
for root, dirs, files in a:
-
output = csv.writer(f, dialect=csv.excel, delimiter=',')
-
output.writerows(a)
-
print "Cycling...", count
-
count += 1
-
f.close()
-
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: - f = open('file_name', 'w')
-
outputList = []
-
for root, dirs, files in os.walk('dir_name'):
-
outputList.append(root)
-
for d in dirs:
-
outputList.append(','.join([root, d]))
-
for f1 in files:
-
outputList.append(','.join([root, f1]))
-
f.write('\n'.join(outputList))
-
f.close()
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
| |