I wrote a very basic program to remove quotation marks from a text file. I have about 50 files I need to run and would rather run the program for all text files in a directory...
The big issue I am having is that I need the files to be read one at a time, and once the " are stripped, all the contents of the file outputted to a new file which uses the name of the input file.
For example:
As I have it now, the program takes the input file name as an argument, and creates the output file name as the infile name + _output.txt. How do I make it so it processes all the text files in a directory but names the files how I want it to?
My program:
import sys
if len(sys.argv) <2:
print "Usage: python %s requires input file" % (sys.argv[0])
sys.exit()
infile = sys.argv[1]
outfile = infile.split(".")[0] + "_output.txt"
INFH = open(infile)
OFH = open(outfile, "w")
print "Output File = %s" % (outfile)
for line in INFH:
line=line.strip('\n').replace('\"','')
print >> OFH, line
INFH.close()
os.listdir(path)andos.path.isfile(path)for this.os.listdir(path)returns files and folders in a directory. Useos.path.isfile(path)to make shure it's a file.