From 7972f8ffc0b5914422b793b0311f202cac1d9f3c Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Fri, 13 Jun 2014 10:27:57 +0300 Subject: [PATCH] create new file if not found --- tsvedit.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/tsvedit.py b/tsvedit.py index 9a96ebd..5752eb9 100755 --- a/tsvedit.py +++ b/tsvedit.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# Copyright 2011 Ville Rantanen +# Copyright 2014 Ville Rantanen # # This program is free software: you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published @@ -20,7 +20,7 @@ __author__ = "Ville Rantanen" -__version__ = "0.1" +__version__ = "0.2" import sys,os import csv @@ -175,16 +175,15 @@ def setup_options(): ''' Setup the command line options ''' parser=ArgumentParser() - parser.add_argument("-v",action='version', version=__version__) - parser.add_argument("--version",action='version', version=__version__) + parser.add_argument("-v","--version",action='version', version=__version__) parser.add_argument("-b",action="store_false",dest="backup",default=True, help="Do not create a backup file.") parser.add_argument("-i",type=str,dest="delimiter",default="\t", - help="Input delimiter for the CSV, default: [tab]") + help="Input delimiter for the TSV, default: [tab]") parser.add_argument("-D",action="store_true",dest="debug",default=False, help="Debug mode, i.e. do not delete the SC file.") - parser.add_argument("csv",type=str,action="store", - help="CSV file to edit") + parser.add_argument("tsv",type=str,action="store", + help="TSV file to edit") options=parser.parse_args() return options @@ -208,7 +207,6 @@ def csv_write(screader,fileout): for row in table: fileout.write('\t'.join(row)+'\n') - if not which('sc'): print('You don\'t seem to have "sc" installed!') print('sudo apt-get install sc') @@ -216,14 +214,19 @@ if not which('sc'): opts=setup_options() -f_bkp=opts.csv+'.bkp' +f_split=os.path.split(opts.csv) +f_bkp=os.path.join(f_split[0],'_bkp.'+f_split[1]) f_sc=opts.csv+'.sc' f_sc_tmp=opts.csv+'.sc.tmp' # copy a backup file -if opts.backup: +if opts.backup and os.path.exists(opts.csv): shutil.copyfile(opts.csv, f_bkp) +# create new empty file if not exist +if not os.path.exists(opts.csv): + open(opts.csv,'w').close() + # Convert CSV -> SC f_sc_w=open(f_sc,'wt') f_csv_r=open(opts.csv,'rt')