4 Replies - 4610 Views - Last Post: 16 January 2012 - 04:13 PM Rate Topic: -----

#1 cthomas1489  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-June 11

Question on writing contents of directory to txt file

Posted 16 January 2012 - 03:22 PM

I'm new to Python and have a quick question.

I'm looking to print the names of the folders/files of my current working directory to a txt file. This is what I have:

import os
output_file = open('names.txt' , 'w')
output_file.write(os.listdir(os.getcwd()))
output_file.close()




Now I know the error lies in the output_file.write function because it is expecting character or string as opposed to what I put. I'm not sure how else to write it though.

Is This A Good Question/Topic? 0
  • +

Replies To: Question on writing contents of directory to txt file

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,151
  • Joined: 07-September 06

Re: Question on writing contents of directory to txt file

Posted 16 January 2012 - 03:29 PM

The problem is that you are trying to use a list as you would a string. So, what you do is simply make the list a string.

Here is an example that puts all the files in a directory into a comma separated string:
"".join(["%s, " % (v) for v in os.listdir(os.getcwd())])


NOTE - You still need to import os.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#3 cthomas1489  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-June 11

Re: Question on writing contents of directory to txt file

Posted 16 January 2012 - 03:37 PM

That worked perfect. There are a few pieces in the code I don't understand though and am hoping you could clarify. Mainly the "".join(["%s, " % (v) for loop ). I know the loop but the .join part is a little confusing.
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,151
  • Joined: 07-September 06

Re: Question on writing contents of directory to txt file

Posted 16 January 2012 - 03:42 PM

Well, join is a string function that is used to join multiple strings together. Because it is a string function we need to put the "" to create an empty string. Then join takes either a string, or a list of strings. So, we loop through each of the filenames we got from the function call and put them into a string in the form of "<String>, " which we put into the list we are going to be adding. The "%s, " % v ... is what does the string part creation for the list.

At least that's my understanding of the code. Hopefully that helps.
Was This Post Helpful? 0
  • +
  • -

#5 cthomas1489  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 20-June 11

Re: Question on writing contents of directory to txt file

Posted 16 January 2012 - 04:13 PM

Sounds good to me! Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1