tags:

views:

125

answers:

3

Here is the code example. Basically output.csv needs to remove any drive letter A:-Y: and replace it with Z: I tried to do this with a list (not complete yet) but it generates the error: TypeError: expected a character buffer object

#!/usr/bin/python
import os.path
import os
import shutil
import csv
import re

# Create the videos directory in the current directory
# If the directory exists ignore it.
#
# Moves all files with the .wmv extenstion to the
# videos folder for file structure
#
#Crawl the videos directory then change to videos driectory
# create the videos.csv file in the videos directory
# replace any drive letter A:-Y: with Z:
def createCSV():
    directory = "videos"
    if not os.path.isdir("." + directory + "/"):
        os.mkdir("./" + directory + "/")
    for file in os.listdir("./"):
        if os.path.splitext(file)[1] == ".wmv":
            shutil.move(file, os.path.join("videos", file))
    listDirectory = os.listdir("videos")
    os.chdir(directory)
    f = open("videos.csv", "w")
    f.writelines(os.path.join(os.getcwd(), f + '\n') for f in listDirectory)
    f = open('videos.csv', 'r')
    w = open('output.csv', 'w')
    f_cont = f.readlines()
    for line in f_cont:
        regex = re.compile("\b[GHI]:")
        re.sub(regex, "Z:", line)
        w.write(line)
        f.close()

createCSV()

EDIT: I think my flow/logic is wrong, the output.csv file that gets created still G: in the .csv it was not renamed to Z:\ from the re.sub line.

+1  A: 

It seems like the problem is in the loop at the bottom of your code. The string's replace method doesn't receive a list as its first arguments, but another string. You need to loop through your removeDrives list and call line.remove with every item in that list.

abyx
A: 

You could use

for driveletter in removedrives:
    line = line.replace(driveletter, 'Z:')

thereby iterating over your list and replacing one of the possible drive letters after the other. As abyx wrote, replace expects a string, not a list, so you need this extra step.

Or use a regular expression like

import re
regex = re.compile(r"\b[FGH]:")
re.sub(regex, "Z:", line)

Additional bonus: Regex can check that it's really a drive letter and not, for example, a part of something bigger like OH: hydrogen group.

Apart from that, I suggest you use os.path's own path manipulation functions instead of trying to implement them yourself.

And of course, if you do anything further with the CSV file, take a look at the csv module.

A commentator above has already mentioned that you should close all the files you've opened. Or use with with statement:

with open("videos.csv", "w") as f:
    do_stuff()
Tim Pietzcker
It's usually more useful to also write /why/ he should use that... not just provide a code snippet :)
abyx
But you already did that. See who got the upvote? :) Yeah, you're right, of course.
Tim Pietzcker
Because I'm still learning programing in general and my logic needs more practice.
Dunwitch
+1  A: 

I can see you use some pythonic snippets, with smart uses of path.join and a commented code. This can get even better, let's rewrite a few things so we can solve your drive letters issue, and gain a more pythonic code on the way :

#!/usr/bin/env python
# -*- coding= UTF-8 -*-

# Firstly, modules can be documented using docstring, so drop the comments
"""
 Create the videos directory in the current directory
 If the directory exists ignore it.

 Moves all files with the .wmv extension to the
 videos folder for file structure

 Crawl the videos directory then change to videos directory
 create the videos.csv file in the videos directory
 create output.csv replace any drive letter A:-Y: with Z:
"""

# not useful to import os and os.path as the second is contain in the first one
import os
import shutil
import csv
# import glob, it will be handy
import glob
import ntpath # this is to split the drive

# don't really need to use a function 

# Here, don't bother checking if the directory exists
# and you don't need add any slash either
directory = "videos"
ext = "*.wmv"
try :
    os.mkdir(directory)
except OSError :
    pass

listDirectory = [] # creating a buffer so no need to list the dir twice

for file in glob.glob(ext): # much easier this way, isn't it ?
        shutil.move(file, os.path.join(directory, file)) # good catch for shutil :-)
        listDirectory.append(file)

os.chdir(directory)

# you've smartly imported the csv module, so let's use it !
f = open("videos.csv", "w")
vid_csv = csv.writer(f)
w = open('output.csv', 'w')
out_csv = csv.writer(w)

# let's do everything in one loop
for file in listDirectory :
    file_path = os.path.abspath(file)
    # Python includes functions to deal with drive letters :-D
    # I use ntpath because I am under linux but you can use 
    # normal os.path functions on windows with the same names
    file_path_with_new_letter = ntpath.join("Z:", ntpath.splitdrive(file_path)[1])
    # let's write the csv, using tuples
    vid_csv.writerow((file_path, ))
    out_csv.writerow((file_path_with_new_letter, ))
e-satis
That was it right there, thank you for getting me on the right path. It's a great example of how to use logic and avoiding cruft.
Dunwitch
Well, the question was nicely written, so you get the answer you deserve.
e-satis