Created
September 16, 2013 18:22
-
-
Save leonardreidy/6584481 to your computer and use it in GitHub Desktop.
Write a list of the contents of the current directory to a csv file in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
# glob does regular expression pattern matching, expansion etc, and, it returns | |
# a list of strings - nice and easy to work with | |
# List files starting with anything and ending with anything: | |
# glob.glob("*.*") | |
# | |
# to list only text files, for example, try: | |
# glob.glob("*.txt") | |
# | |
# or, for pdfs try: | |
# glob.glob("*.pdf") | |
# Store list of directories contents to a variable (globlet) | |
globlet = glob.glob("*.*") | |
# get the length of the variable for determining when to stop adding | |
# commas to our resulting comma-separated file | |
globletSize = len(globlet) | |
# open an appropriately named file for writing, | |
# and then use the csv module to create the output | |
# in said file! | |
# | |
# Note, if you want to have quotes around each element of the list | |
# use the following syntax: | |
# w = csv.writer(file, quoting=csv.QUOTE_ALL) | |
with open('outfile.txt', 'w') as file: | |
w = csv.writer(file, quoting=csv.QUOTE_NONE) | |
w.writerow(globlet) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Import missing ?
import csv