I am trying to write a python script to open file, read each line, and replace the 18th position of each line with a a user specified character (sysargv) IF AND ONLY IF that 18th position is another user specified character (sysargv). It should then save it as a file "renamed" For some reason Python runs amok. In some instances it renames every occurence of the user specified character regardless of position then in other lines it does not rename any of them, even if they are at position 18. My code is below. I can not find anything wrong with it. Can anyone see why Python might be misinterpreting my instructions so much?
#!/usr/bin/python import sys bh = "renamed" + sys.argv wh = sys.argv xc = sys.argv dh = open(sys.argv) nh = open(bh,'w') for line in dh: if line[17:18] == wh : line = line[17:18].replace(wh, xc) nline = nh.write(line) print line else : nline = nh.write(line) dh.close()