I want to know how can I get the string
from terminal
or command prompt
and make lower case that string and display it on terminal
or command prompt
I used this code but it's not working
// in main.py cmd or terminal
import sys
tdf = sys.argv[1]
print(tdf)
and in terminal
// in cmd or terminal
PS E:\Python\Project\At> python .\main.py "Hi"
when I used this in my terminal I got this error
File "C:\\Users\\miladm\\AppData\\Local\\Programs\\Python\\Python39\\lib\\encodings\\cp1256.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\ی' in position 13: character maps to
Code:
import sys
print ("Data from terminal :",sys.argv)
while running the script use:
D:\python>python cmdline.py "hello"
Output:
Data from terminal : ['cmdline.py', 'hello']
run with python filename.py "arguments"
instead of
tdf = sys.argv[1]
Use,
tdf = sys.argv[-1]
because it will get the last argument on the command line. If no arguments are passed, it will be the script name itself, as sys.argv[0] is the name of the running program. in your case if there is no arguments passed it will create an error
import sys
tdf = sys.argv[-1]
print(tdf)
To read user input you can try the cmd
module for easily creating a mini-command line interpreter (with help texts and autocompletion) and raw_input
(input for Python 3+) for reading a line of text from the user.
text = raw_input("prompt") # Python 2
text = input("prompt") # Python 3
Command line inputs are in sys.argv. Try this in your script:
import sys
print (sys.argv)
There are two modules for parsing command line options: optparse
(deprecated since Python 2.7, use argparse instead) and getopt. If you just want to input files to your script, behold the power of fileinput.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.