-1

I wrote a simple script that reads text from a file and writes it to a new file. It currently functions by prompting the user to enter the name of the file, but I would like to save time and include that as part of program execution.

lattice = []
coords = []
file = input("Enter name of output file: ")

with open(file) as openfile:
    for line in openfile:
            if ("acell" in line) and ("Bohr" in line):
                Sort = line.split()
                for value in Sort:
                    lattice.append(value)

with open(file, 'r+') as f:
    for line in f:
        if ("xred" in line) and ("E" in line):
            coords.append(line)
            for i in range(5):
                line = f.readline()
                coords.append(line)

Out = file.replace('.out','.coords')
g = open(Out,"w+")

g.write("\n*********************************************************\n\n")

g.write("Initial xred \n\n")
for i in range(len(coords)+1):

    if i <= 5:
        g.write(str(coords[i]))

    if i == 6:
        g.write("Final xred\n\n")

    if i >= 7:
        g.write(str(coords[i-1]))

g.write("Initial acell \n\n")
g.write(str(lattice[1]+' '+str(lattice[2]+' '+str(lattice[3])+'\n')))

g.write("\nFinal acell \n\n")
g.write(str(lattice[6]+' '+str(lattice[7]+' '+str(lattice[8])+'\n\n')))

g.write("\n*********************************************************\n\n")

g.close()

Would it be possible for the user to enter "python program.py file.out" all in one command in order to produce file.coords?

0

1 Answer 1

0

Yes simply use sys.argv[1] (0 is the name of the file itself)

from sys import argv
lattice = []
coords = []
try:
    file = argv[1]
except IndexError as e:
     # some error handling in case the file is not called with an argument

with open(file) as openfile:
    for line in openfile:
            if ("acell" in line) and ("Bohr" in line):
                Sort = line.split()
                for value in Sort:
                    lattice.append(value)

with open(file, 'r+') as f:
    for line in f:
        if ("xred" in line) and ("E" in line):
            coords.append(line)
            for i in range(5):
                line = f.readline()
                coords.append(line)

Out = file.replace('.out','.coords')
g = open(Out,"w+")

g.write("\n*********************************************************\n\n")

g.write("Initial xred \n\n")
for i in range(len(coords)+1):

    if i <= 5:
        g.write(str(coords[i]))

    if i == 6:
        g.write("Final xred\n\n")

    if i >= 7:
        g.write(str(coords[i-1]))

g.write("Initial acell \n\n")
g.write(str(lattice[1]+' '+str(lattice[2]+' '+str(lattice[3])+'\n')))

g.write("\nFinal acell \n\n")
g.write(str(lattice[6]+' '+str(lattice[7]+' '+str(lattice[8])+'\n\n')))

g.write("\n*********************************************************\n\n")

g.close()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.