0

G'day all,

I am just starting to teach myself python and in the program below I am reading in a CSV file and moving files from one directory to another. I would like to be able to ensure that the user enters a Y or an N only, if they enter any other character I would like to display a message saying "You didn't enter Y or N !" then ask them to enter Y or N again. This should keep looping until they enter Y or N.

Is there a way to do this

Thanks in Advance

Eclipse

"""
Author: P Smyth
Date:   17 May 2008
This program reads in a CSV file in the following format
filename, current directory, target directory
checks to see if the target directory exists,
if not creates the target directory.  Then copies the files
from the current directory to the target directory

Amended by: P Smyth
Amended Date: 17 May 2008
Reason:  Added functionality to check whether the filename
already exists in the directory.  If it already exists, 
asks user if they would like to overwrite the file

Amended by: P Smyth
Amended Date: 17 May 2008
Reason:  changed the user input for overwriting files from
raw_input to listen for a key (y or n)
"""

import csv, os, shutil, msvcrt

os.system("cls") # clears the command window

readlist = []
dirlist = []

reader = csv.reader(open("D:\\Java\\testcsv.csv", "rb"))# opens the csv file required
for row in reader:
    # creates a list containing the information from each line of the CSV file
    readlist.append(row)

for each in readlist:
    dirdict = {'curpath': os.path.join(each[1], each[0]), 'targetpath': os.path.join(each[2], each[0])}
    dirlist.append(dirdict)

for line in dirlist:
    targetdir = os.path.split(line['targetpath'])
    if os.path.isdir(targetdir[0]):
        if os.path.isfile(line['targetpath']):
            print line['targetpath'], "File exists..."
            print "Would you like to overwrite the file: press y or n"
            a = msvcrt.getch()
            while a.lower() != "y" or a.lower() != "n":
                print "!!!!!!!! You didn't press y or n !!!!!!!!"
                print "Would you like to overwrite the file: press y or n"
                a = msvcrt.getch()
            else:
                if a.lower() == "y":
                    shutil.copyfile( line['curpath'], line['targetpath'])
                    print "copying file", line['curpath'], "to", line['targetpath']
                elif a.lower() == "n":
                    break
        else:        
            print "copying file", line['curpath'], "to", line['targetpath']
            print "Directory exists"
            shutil.copyfile( line['curpath'], line['targetpath'])
            print "copying file", line['curpath'], "to", line['targetpath']
    else:
        print "Directory does not exist, Creating directory", targetdir[0]
        os.mkdir(targetdir[0])
        print "Copying file", line['curpath'], "to", line['targetpath']
        shutil.copyfile( line['curpath'], line['targetpath'])

print "\nCopying finished"
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Freaky_Chris
0

Indeed there is

while True:
  overWrite = raw_input("Would you like to overwrite log file? (y/n)->")
  if overWrite.upper() == "Y":
     break
  elif overWrite.upper() == "N":   
     break
  else:
     print "Invalid Option\n"

ignore naming of variables etc, thats just taken from something im working on myself right now.

Chris

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.