create new file if not found
This commit is contained in:
23
tsvedit.py
23
tsvedit.py
@@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python
|
#!/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
|
# 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
|
# under the terms of the GNU Lesser General Public License as published
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
__author__ = "Ville Rantanen"
|
__author__ = "Ville Rantanen"
|
||||||
|
|
||||||
__version__ = "0.1"
|
__version__ = "0.2"
|
||||||
|
|
||||||
import sys,os
|
import sys,os
|
||||||
import csv
|
import csv
|
||||||
@@ -175,16 +175,15 @@ def setup_options():
|
|||||||
''' Setup the command line options '''
|
''' Setup the command line options '''
|
||||||
|
|
||||||
parser=ArgumentParser()
|
parser=ArgumentParser()
|
||||||
parser.add_argument("-v",action='version', version=__version__)
|
parser.add_argument("-v","--version",action='version', version=__version__)
|
||||||
parser.add_argument("--version",action='version', version=__version__)
|
|
||||||
parser.add_argument("-b",action="store_false",dest="backup",default=True,
|
parser.add_argument("-b",action="store_false",dest="backup",default=True,
|
||||||
help="Do not create a backup file.")
|
help="Do not create a backup file.")
|
||||||
parser.add_argument("-i",type=str,dest="delimiter",default="\t",
|
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,
|
parser.add_argument("-D",action="store_true",dest="debug",default=False,
|
||||||
help="Debug mode, i.e. do not delete the SC file.")
|
help="Debug mode, i.e. do not delete the SC file.")
|
||||||
parser.add_argument("csv",type=str,action="store",
|
parser.add_argument("tsv",type=str,action="store",
|
||||||
help="CSV file to edit")
|
help="TSV file to edit")
|
||||||
options=parser.parse_args()
|
options=parser.parse_args()
|
||||||
return options
|
return options
|
||||||
|
|
||||||
@@ -208,7 +207,6 @@ def csv_write(screader,fileout):
|
|||||||
for row in table:
|
for row in table:
|
||||||
fileout.write('\t'.join(row)+'\n')
|
fileout.write('\t'.join(row)+'\n')
|
||||||
|
|
||||||
|
|
||||||
if not which('sc'):
|
if not which('sc'):
|
||||||
print('You don\'t seem to have "sc" installed!')
|
print('You don\'t seem to have "sc" installed!')
|
||||||
print('sudo apt-get install sc')
|
print('sudo apt-get install sc')
|
||||||
@@ -216,14 +214,19 @@ if not which('sc'):
|
|||||||
|
|
||||||
opts=setup_options()
|
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=opts.csv+'.sc'
|
||||||
f_sc_tmp=opts.csv+'.sc.tmp'
|
f_sc_tmp=opts.csv+'.sc.tmp'
|
||||||
|
|
||||||
# copy a backup file
|
# copy a backup file
|
||||||
if opts.backup:
|
if opts.backup and os.path.exists(opts.csv):
|
||||||
shutil.copyfile(opts.csv, f_bkp)
|
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
|
# Convert CSV -> SC
|
||||||
f_sc_w=open(f_sc,'wt')
|
f_sc_w=open(f_sc,'wt')
|
||||||
f_csv_r=open(opts.csv,'rt')
|
f_csv_r=open(opts.csv,'rt')
|
||||||
|
|||||||
Reference in New Issue
Block a user