Constructive criticism EX16 ARGV function

Sup guys, again like my other post, trying to write argv function and the whole scenario with looking Zeds work. I have ofcourse changed it to.

feedback please, improvements ect…

from sys import argv

script, filename = argv
txt = open(filename)
print("Thank you for executing file '{}', below are the details contained within the 
file".format(filename))
print(txt.read())
txt = open(filename, 'w')
print("We will now delete the content within the file.")
txt.truncate()
print("File content deleted!")
print("Please add new content to the file please. (MAX 3 lines)")
line_1 = input("Line 1: ")
line_2 = input("Line 2: ")
line_3 = input("Line 3: ")

print("Adding the new content to file '{}' now...".format(filename))

txt.write(line_1)
txt.write("\n")
txt.write(line_2)
txt.write("\n")
txt.write(line_3)
txt.write("\n")

txt.close()

print("Added. If you would like to open the file, enter file name below.")
print("If NOT press CTRL-Z")
txt_again = input("> ")

file_again = open(txt_again)

print(file_again.read())

file_again.close()

If you are using Python 3, how about using f-strings? It might make it a little easier to read.

Yes, I second that @Kamiownz. Convert your .format() calls to f-strings. You’ll see it looks better. Only caveat is you need python 3.6 or better.

I tried to use f-strings but they seem to constantly give me errors when I was executed code using the -i function. and didnt think to change f-string after i figured that error.

Just tried it now and it works :)…I’ll go through and make them all f-strings…

THANKS!

I keep seeing people using the -i option. Why are you doing that? i have never ever used that option.

I see that used in another book I’m reading a lot. The author will create a class in a file, and then run it from cli with -i option so it immediately drops into the interpreter.

For me it’s an odd work flow as I either want to be interpreter hacking about, or working in files and running from cli. Perhaps the idea is that noobs may make a lot of interpreter errors setting up the class and having to rewrite the class each time may be demotivating. I’m not sure. But given initially switching between interpreter and modules/files can confuse people, it’s not the best advice.

Of course, we know a quick up arrow lists through previous entered commands which makes the rewrite a lot quicker, and anyone using an IDE like PyCharm will have an enhanced interpreter anyway. But if there is a legitimate use case for this feature (which I assume there must be) then @zedshaw I’d love to know what it is.

I run a ‘penny bot’ which reprices my products for me. So I’m so use to executing code with -i command

Cool. Can you give a practical example of why you drop to the interpreter after execution of the penny bot? I’m keen to understand the usage.

You might want to check out:

https://docs.python.org/3/library/readline.html

Maybe you can do your own nicer interactive shell to do this instead of using -i. Either way, -i seems to be causing you a lot of trouble so it might be time to avoid it.