Invalid literal for int() with base 16 - python

I have a problem with the below code. I am running python script to run exe and pass parameters to it. I get the below error:
ValueError invalid literal for int() with base 16:
However if i put an extra semi colon (which I don't need) after 0 and before double quote it will work. Is something wrong with my string?
program="C:\Program Files (x86)\program.exe"
args='"21022019;A0A1A2A3A4A5A6A7;B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF;1;1982;0"-e fixed -k aa11bb22cc33dd44ee55ff6600112233 -f jf_Creds_python.hex'
subprocess.call([program,args])
This is the full error:
Traceback (most recent call last):
File "<string>", line 249, in <module>
File "<string>", line 53, in main
ValueError: invalid literal for int() with base 16: '"21022019'

I have found a fix but do not really understand it.
program="C:\Program Files (x86)\program.exe"
args='"21022019;A0A1A2A3A4A5A6A7;B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF;1;1982;0"-e fixed -k aa11bb22cc33dd44ee55ff6600112233 -f jf_Creds_python.hex'
subprocess.call([program,{args}])
If I place curly braces around args, it will be successful. It works now, but I'm not fully sure how it is working, if someone might explain it would be great?

Related

Same code differs in output across IDE's & pyshell

I was running this code on VScode.
age_1 = int(input("age : "))
print(age_1)
It shows this at it's terminal output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'print(age_1)
In Pycharm it shows this output after taking input as 12 :
age : 12
12
In PyShell it works like the Pycharm one. What did I do wrong in the VScode ?
Update :
When ran on a new file on VScode, it ran properly like the Pycharm one. But only for once . Then it shows up same error like before along with some new kinds of error everytime it ran.
Here's the correct one in VScode when ran the 1st time.
PS I:\CSE\LEARNING\PYTHON 3.0\practice> & "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
age : 12
12
Here's one error when ran another time.
& "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
File "<stdin>", line 1
& "C:/Users/Alvi Adhikary Niloy/AppData/Local/Programs/Python/Python39/python.exe" "i:/CSE/LEARNING/PYTHON 3.0/practice/tempCodeRunnerFile.py"
^
SyntaxError: invalid syntax
Here's another error :
>>>print(age_1) age1 = int(input("age : "))
File "<stdin>", line 1
print(age_1) age1 = int(input("age : "))
^
SyntaxError: invalid syntax
& here's the final one .
>>> print(age1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'age1' is not defined
P.S. I ain't skilled in coding. Explaining with some codes patiently will be appretiated.
The Python ValueError: invalid literal for int() with base 10 error is raised when you try to convert a string value that is not formatted as an integer. ... Then, you can use int() to convert your number to an integer. If this does not work, make sure that the value of a string does not contain any letters.I think you wrote a word that does not change int in the input. Maybe it's a space

Trying to make a log function

I want to make a log function in my script but I don`t know why it gives me this error
File "c:/Users/x/x/x", line 33
log = open(f"Log/{realDate.strftime("%x")}.txt", "a")
^
SyntaxError: invalid syntax
Here`s the code that is causing the problem
realDate = datetime.datetime.now()
log = open(f"Log/{realDate.strftime("%x")}.txt", "a")
log.write("Hello Stackoverflow!")
log.close()
Can somebody please help me?
The problem is that you are trying to nest double quotes inside double quotes; f-strings don't allow that. You would need to change one or the other to another style of quotes. For example:
log = open(f'Log/{realDate.strftime("%x")}.txt', "a")
The main problem is that you aren't using a version of Python that supports f-strings. Make sure you are using Python 3.6 or later. (Update: Even in Python 3.6 or later, the identified location of the error can shift:
>>> f"{print("%x")}"
File "<stdin>", line 1
f"{print("%x")}"
^
SyntaxError: invalid syntax
>>> f"{print("x")}"
File "<stdin>", line 1
f"{print("x")}"
^
SyntaxError: invalid syntax
)

python console to call function

I know this is an easy fix, but could someone tell me how to call a python file in the python Console who have this symbol: -.
Here is my mistake:
>>> import main #no error here
>>> import a1-devoir1
File "<input>", line 1
import a1-devoir1
Syntax Error: invalid syntax
You must name your files so that they only contains letters, underscores, or numbers (but not as the first character). All libraries and modules must follow this.
So rename your .py file to a1_devoir and then try import a1_devoir

Pytorch Cityscapes Dataset, train_distribute problem - "Typeerror: path should be string, bytes, pathlike or integer, not NoneType"

I'm very unfamiliar with Machine Learning, python, and such, so forgive my oblivious errors. I'm trying to use machine learning systems on a dataset of streetscapes I have. I found a lot or resources, and I'm working off of this package which has a lot of examples and seems straightforward.
When I attempted to run the train_distribute.py file, I received this error:
(base) corey#corona:~/Desktop/pycity/GALD-Net-master$ python train_distribute.py
Traceback (most recent call last):
File "train_distribute.py", line 261, in <module>
main()
File "train_distribute.py", line 136, in main
if not os.path.exists(args.save_dir):
File "/home/corey/anaconda3/lib/python3.7/genericpath.py", line 19, in exists
os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
Looking in the code, it's coming from these lines:
def main():
# make save dir
if args.local_rank == 0:
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
# launch the logger
Log.init(
log_level=args.log_level,
I'm guessing this means I need a more exact file structure, and to point the code at the right location. I am in no way a computer scientist and have close to zero understanding of what does what and how things like this work. Any advice for what I'm doing wrong and how I can approach fixing things?
From the error message, my guess would be that args.save_dir is None. os.path.exists cannot deal with None as a path:
>>> import os
>>> os.path.exists(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/genericpath.py", line 19, in exists
os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
Looking at the script you cited, the save_dir argument has a default value of None. It might be useful to make this a required argument and remove the default value, since the main function depends on it.
You have to specify the save_dir as an argument.
Run the code like this
python train_distribute.py --save_dir=SAVE_PATH
Here SAVE_PATH will take the path where you want to get the outputs saved.
Also, note that if the folder specified by the given path does not exist then that folder will be created.

Escaping Special Characters (*) in Python subprocess.call()

I can't seem to be able to escape these special characters - *.
I'm trying to copy some files using a Python script with the following line
subprocess.call(r"mkdir E:\CONTENT\Full1",shell=True)
subprocess.call(r"copy","E:\DATA FOR CONTENT\*.*","E:\CONTENT\Full1",shell=True)
subprocess.call(r"fsutil file createnew ","E:\CONTENT\versionfile.txt","6500000",shell=True)
But I get the following error at the 2nd line
Traceback (most recent call last):
File "basic1.py", line 9, in <module>
subprocess.call(r"copy","E:\DATA FOR CONTENT\*.*","E:\CONTENT\Full1",shell=True)
File "C:\Python34\lib\subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Python34\lib\subprocess.py", line 767, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
Also I hate passing it using the CSV Style ("arg1","arg2"...). Is there any way of passing it as a raw string?
args should be a sequence of program arguments or else a single string.
subprocess.call(["copy", r"E:\DATA FOR CONTENT\*.*", r"E:\CONTENT\Full1"], shell=True)
Better don't do this via subprocess. Believe me it's a bad idea.
For simple filesystem copy operations take a look on python shutil module.
For creating new directories use os.makedirs.
For creating files of certain size see this answers.

Categories