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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Eclipse
    New Member
    • Dec 2007
    • 6

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

    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:\Documen ts and Settings\Smythv ille\My Documents\Pytho n\Stuff.csv", "wb"))

    for root, dirs, files in os.walk('C:\Doc uments and Settings\Smythv ille\My Documents\BitTo rrent Downloads'):
    writer.writerow s(root, dirs, files)
    f.close()

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

    Traceback (most recent call last):
    File "C:\Documen ts and Settings\Smythv ille\My Documents\Pytho n\Stuff\Walkdir _clean.py", line 7, in <module>
    writer.writerow s(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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by 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:\Documen ts and Settings\Smythv ille\My Documents\Pytho n\Stuff.csv", "wb"))

    for root, dirs, files in os.walk('C:\Doc uments and Settings\Smythv ille\My Documents\BitTo rrent Downloads'):
    writer.writerow s(root, dirs, files)
    f.close()

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

    Traceback (most recent call last):
    File "C:\Documen ts and Settings\Smythv ille\My Documents\Pytho n\Stuff\Walkdir _clean.py", line 7, in <module>
    writer.writerow s(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:[code=Python]import os
    a = os.walk(directo ry_name)

    for root, dir, file in a:

    print "Root directory: %s" % (root)

    if dir:
    print "Subdirecto ries 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(r oot, x)), file)
    fileStr = "".join(fileLis t)
    print fileStr
    else:
    print "There are no files in directory %s\n" % (root)[/code]

    Comment

    • Eclipse
      New Member
      • Dec 2007
      • 6

      #3
      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,filen ame2]).

      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,fil e1
      path\subdir,fil e2
      path\subdir\sub subdir1,file1
      path\subdir\sub subdir2,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???

      [CODE=PYTHON] import os
      import csv
      a = os.walk('C:\Doc uments and Settings\Smythv ille\My Documents\BitTo rrent Downloads')
      f = open('C:\Docume nts and Settings\Smythv ille\My Documents\Pytho n\Stuff.csv', 'w')
      count = 1
      for root, dirs, files in a:
      output = csv.writer(f, dialect=csv.exc el, delimiter=',')
      output.writerow s(a)
      print "Cycling... ", count
      count += 1
      f.close()
      [/CODE]

      I put the count and print commands in to see if the code was iterating but it didn't

      Thanks for your help

      Eclipse

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by Eclipse
        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,filen ame2]).

        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,fil e1
        path\subdir,fil e2
        path\subdir\sub subdir1,file1
        path\subdir\sub subdir2,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???

        [CODE=PYTHON] import os
        import csv
        a = os.walk('C:\Doc uments and Settings\Smythv ille\My Documents\BitTo rrent Downloads')
        f = open('C:\Docume nts and Settings\Smythv ille\My Documents\Pytho n\Stuff.csv', 'w')
        count = 1
        for root, dirs, files in a:
        output = csv.writer(f, dialect=csv.exc el, delimiter=',')
        output.writerow s(a)
        print "Cycling... ", count
        count += 1
        f.close()
        [/CODE]

        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: [code=Python]f = open('file_name ', 'w')
        outputList = []
        for root, dirs, files in os.walk('dir_na me'):
        outputList.appe nd(root)
        for d in dirs:
        outputList.appe nd(','.join([root, d]))
        for f1 in files:
        outputList.appe nd(','.join([root, f1]))
        f.write('\n'.jo in(outputList))
        f.close()[/code]

        Comment

        Working...