python command line argument [duplicate] - python

This question already has an answer here:
How to get bash arguments with leading pound-sign (octothorpe)
(1 answer)
Closed 1 year ago.
I am trying to pass a command line argument like this because the actual string contains a # symbol before and after it
python3 test.py #fdfdf#
This is the error message I am getting
Traceback (most recent call last):
File "test.py", line 3, in <module>
print(sys.argv[1])
IndexError: list index out of range
How can I pass a string which contains a # symbol in the beginning and the end as a command line argument?
Update:
OS: Ubuntu 20.04

You can try one of these and see which one works:
python3 test.py "#fdfdf#" or python3 test.py \#fdfdf\#

Related

new line issue with f.write [duplicate]

This question already has answers here:
Writing string to a file on a new line every time
(19 answers)
Closed 3 months ago.
I'm trying to use the f.write keyword, I want each thing I write to be in a new line so I did this:
f.write('',message_variable_from_previous_input,'\n')
However, after I ran this it threw back an error saying the following:
Traceback (most recent call last):
File "c:\Users\User1\OneDrive\Desktop\coding\folder_namr\file_name.py", line 5, in <module>
f.write('',msg,'\n')
TypeError: TextIOWrapper.write() takes exactly one argument (3 given)
Does anybody know haw to fix this?
write method takes exactly one argument
so you should write like this:
f.write(f"{message_variable_from_previous_input}\n")
or:
f.write(str(message_variable_from_previous_input) + "\n")

Why I can't manually call `next` on `file` iterator? [duplicate]

This question already has answers here:
Attribute Error: next()
(2 answers)
Is generator.next() visible in Python 3?
(3 answers)
Closed 4 years ago.
This is my Python code. If the line has : I'm want to skip this line and the next line. I'm trying to use file.next since file is an iterator. Why is it not working?
file=open("example.txt","r") //output of ls -R
currentDirectory=""
for line in file:
if ":" in line:
currentDirectory=line[:-1]
file.next() # Error in this line
continue
A=line.split()
print(A)
print(len(A))
print('insert into files set name=${name}, inode=${inode}, isDir=${isDir},size=${size}'
.format(inode=A[0],name=A[9],isDir="d" in A[1],size=A[5]))
The error:
Traceback (most recent call last):
File "ls-to-sql.py", line 7, in <module>
file.next()
AttributeError: '_io.TextIOWrapper' object has no attribute 'next
I'm sure there are many other ways to implement the same logic. The question is: why this is not working. And if there is another iteration way to solve it.

How can I handle index when reading from stdin [duplicate]

This question already has answers here:
embedding short python scripts inside a bash script
(9 answers)
Closed 4 years ago.
I'm working with bash scripts and would like to embed a Python snippet inside a bash function.
So I got this working Python snippet, which simply reads from stdin and parse it to get the title of the entry[0]:
import feedparser, sys
root = feedparser.parse(sys.stdin.read())
print root['entries'][0].title
And everything seems fine with it:
$ curl -sf https://feedforall.com/sample.xml | python xmlparser.py
RSS Solutions for Restaurants
An IndexError happens when I execute this way:
$ curl -sf https://feedforall.com/sample.xml | python - <<EOF
import feedparser, sys
root = feedparser.parse(sys.stdin.read())
print root['entries'][0].title
EOF
Got this IndexError:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
IndexError: list index out of range
Seems like root['entries'] in this case is returning an empty list..and I don't know why.
Thanks for help
Worked fine executing like that:
$ curl -sf https://feedforall.com/sample.xml | python <( cat <<EOF
import feedparser, sys
root = feedparser.parse(sys.stdin.read())
print root['entries'][0].title
EOF
)

error for input() function in python [duplicate]

This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 7 years ago.
I'm a Python newbie and I get the following error for the code...
name = input("What's your name? ")
print(name)
Output:
G:\Code\Python>UserInput.py
What's your name? Jeremy
Traceback (most recent call last):
File "G:\Code\Python\UserInput.py", line 3, in <module>
name = input("What's your name? ")
File "<string>", line 1, in <module>
NameError: name 'Jeremy' is not defined
The code gets executed only if I replace the input() as raw_input... How do I get it to display the message just by including the input()? I get that it has got something to do with the Python Interpreter versions (I've got both python27 and python34 installed). How do I go on about that??
You shouldn't be using input, it is equivalent to eval(raw_input(prompt)) which will try to execute your input.
https://docs.python.org/2/library/functions.html#input
Use raw_input instead.

Running google_sql I get invalid syntax error [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 8 years ago.
Running:
Google Cloud SDK 0.9.9
Python 3.3
When I execute the below:
c:\google-cloud-sdk-0.9.9\bin>google_sql reaperfire-cloud-sql
I get the below error:
Traceback (most recent call last):
File "c:\google-cloud-sdk-0.9.9\bin\gauth", line 10, in <module>
import bootstrapping.bootstrapping as bootstrapping
File "c:\google-cloud-sdk-0.9.9\bin\bootstrapping\bootstrapping.py", line 251
print 'There are currently no authorized credentials.',
^
SyntaxError: invalid syntax
In python 3 print is a function so you have to write it like this:
print("Hello, World!")
in 2 its like this:
print 'Hello, World!'
look here for more on Printing in Python 3
google_sql requires Python 2, and not 3...downloading python 2.x worked. thanks.

Categories