Change hardcoded values in python 3
up vote
0
down vote
favorite
I would like to change hardcoded values in my code. I would like the code to replace and change hardcoded values based on the number of times it runs. Beginning with:
x=1
The next time, after I run it, in the code itself, I would like to see in the code editor:
x=2
It will automatically change the values of the code without human input, so the third time its run:
x=3
And this is all done just by the script running, no human interaction whatsoever. Is there an easy way?
python-3.x hardcode
add a comment |
up vote
0
down vote
favorite
I would like to change hardcoded values in my code. I would like the code to replace and change hardcoded values based on the number of times it runs. Beginning with:
x=1
The next time, after I run it, in the code itself, I would like to see in the code editor:
x=2
It will automatically change the values of the code without human input, so the third time its run:
x=3
And this is all done just by the script running, no human interaction whatsoever. Is there an easy way?
python-3.x hardcode
One option would be to open in write mode your .py file at the end of your script, find the line you want to modify and replace the value.
– Patol75
Nov 20 at 7:49
2
Possible, of course - most things are. Desirable? Not really. If this value needs to persist across runs, then look at storing it in a file which you can load from your script each time.
– SiHa
Nov 20 at 7:50
Sure they are possible, but is there an easy way? I would like the values to be dynamic , like that 1 become 2. Let me rephrase it
– Durian Jaykin
Nov 20 at 7:51
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to change hardcoded values in my code. I would like the code to replace and change hardcoded values based on the number of times it runs. Beginning with:
x=1
The next time, after I run it, in the code itself, I would like to see in the code editor:
x=2
It will automatically change the values of the code without human input, so the third time its run:
x=3
And this is all done just by the script running, no human interaction whatsoever. Is there an easy way?
python-3.x hardcode
I would like to change hardcoded values in my code. I would like the code to replace and change hardcoded values based on the number of times it runs. Beginning with:
x=1
The next time, after I run it, in the code itself, I would like to see in the code editor:
x=2
It will automatically change the values of the code without human input, so the third time its run:
x=3
And this is all done just by the script running, no human interaction whatsoever. Is there an easy way?
python-3.x hardcode
python-3.x hardcode
edited Nov 20 at 9:19
Jebby
1,023418
1,023418
asked Nov 20 at 7:46
Durian Jaykin
126
126
One option would be to open in write mode your .py file at the end of your script, find the line you want to modify and replace the value.
– Patol75
Nov 20 at 7:49
2
Possible, of course - most things are. Desirable? Not really. If this value needs to persist across runs, then look at storing it in a file which you can load from your script each time.
– SiHa
Nov 20 at 7:50
Sure they are possible, but is there an easy way? I would like the values to be dynamic , like that 1 become 2. Let me rephrase it
– Durian Jaykin
Nov 20 at 7:51
add a comment |
One option would be to open in write mode your .py file at the end of your script, find the line you want to modify and replace the value.
– Patol75
Nov 20 at 7:49
2
Possible, of course - most things are. Desirable? Not really. If this value needs to persist across runs, then look at storing it in a file which you can load from your script each time.
– SiHa
Nov 20 at 7:50
Sure they are possible, but is there an easy way? I would like the values to be dynamic , like that 1 become 2. Let me rephrase it
– Durian Jaykin
Nov 20 at 7:51
One option would be to open in write mode your .py file at the end of your script, find the line you want to modify and replace the value.
– Patol75
Nov 20 at 7:49
One option would be to open in write mode your .py file at the end of your script, find the line you want to modify and replace the value.
– Patol75
Nov 20 at 7:49
2
2
Possible, of course - most things are. Desirable? Not really. If this value needs to persist across runs, then look at storing it in a file which you can load from your script each time.
– SiHa
Nov 20 at 7:50
Possible, of course - most things are. Desirable? Not really. If this value needs to persist across runs, then look at storing it in a file which you can load from your script each time.
– SiHa
Nov 20 at 7:50
Sure they are possible, but is there an easy way? I would like the values to be dynamic , like that 1 become 2. Let me rephrase it
– Durian Jaykin
Nov 20 at 7:51
Sure they are possible, but is there an easy way? I would like the values to be dynamic , like that 1 become 2. Let me rephrase it
– Durian Jaykin
Nov 20 at 7:51
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You can simply write to a well-defined auxiliary file:
# define storage file path based on script path (__file__)
import os
counter_path = os.path.join(os.path.dirname(__file__), 'my_counter')
# start of script - read or initialise counter
try:
with open(counter_path, 'r') as count_in:
counter = int(count_in.read())
except FileNotFoundError:
counter = 0
print('counter =', counter)
# end of script - write new counter
with open(counter_path, 'w') as count_out:
count_out.write(str(counter + 1))
This will store an auxiliary file next to your script, which contains the counter
verbatim.
$ python3 test.py
counter = 0
$ python3 test.py
counter = 1
$ python3 test.py
counter = 2
$ cat my_counter
3
add a comment |
up vote
2
down vote
Use config parser to store run counter in a file
import configparser
config = configparser.ConfigParser()
config_fn = 'program.ini'
try:
config.read(config_fn)
run_counter = int(config.get('Main', 'run_counter'))
except configparser.NoSectionError:
run_counter = 0
config.add_section('Main')
config.set('Main', 'run_counter', str(run_counter))
with open(config_fn, 'w') as config_file:
config.write(config_file)
run_counter += 1
print("Run counter {}".format(run_counter))
config.set('Main', 'run_counter', str(run_counter))
with open(config_fn, 'w') as config_file:
config.write(config_file)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can simply write to a well-defined auxiliary file:
# define storage file path based on script path (__file__)
import os
counter_path = os.path.join(os.path.dirname(__file__), 'my_counter')
# start of script - read or initialise counter
try:
with open(counter_path, 'r') as count_in:
counter = int(count_in.read())
except FileNotFoundError:
counter = 0
print('counter =', counter)
# end of script - write new counter
with open(counter_path, 'w') as count_out:
count_out.write(str(counter + 1))
This will store an auxiliary file next to your script, which contains the counter
verbatim.
$ python3 test.py
counter = 0
$ python3 test.py
counter = 1
$ python3 test.py
counter = 2
$ cat my_counter
3
add a comment |
up vote
0
down vote
accepted
You can simply write to a well-defined auxiliary file:
# define storage file path based on script path (__file__)
import os
counter_path = os.path.join(os.path.dirname(__file__), 'my_counter')
# start of script - read or initialise counter
try:
with open(counter_path, 'r') as count_in:
counter = int(count_in.read())
except FileNotFoundError:
counter = 0
print('counter =', counter)
# end of script - write new counter
with open(counter_path, 'w') as count_out:
count_out.write(str(counter + 1))
This will store an auxiliary file next to your script, which contains the counter
verbatim.
$ python3 test.py
counter = 0
$ python3 test.py
counter = 1
$ python3 test.py
counter = 2
$ cat my_counter
3
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can simply write to a well-defined auxiliary file:
# define storage file path based on script path (__file__)
import os
counter_path = os.path.join(os.path.dirname(__file__), 'my_counter')
# start of script - read or initialise counter
try:
with open(counter_path, 'r') as count_in:
counter = int(count_in.read())
except FileNotFoundError:
counter = 0
print('counter =', counter)
# end of script - write new counter
with open(counter_path, 'w') as count_out:
count_out.write(str(counter + 1))
This will store an auxiliary file next to your script, which contains the counter
verbatim.
$ python3 test.py
counter = 0
$ python3 test.py
counter = 1
$ python3 test.py
counter = 2
$ cat my_counter
3
You can simply write to a well-defined auxiliary file:
# define storage file path based on script path (__file__)
import os
counter_path = os.path.join(os.path.dirname(__file__), 'my_counter')
# start of script - read or initialise counter
try:
with open(counter_path, 'r') as count_in:
counter = int(count_in.read())
except FileNotFoundError:
counter = 0
print('counter =', counter)
# end of script - write new counter
with open(counter_path, 'w') as count_out:
count_out.write(str(counter + 1))
This will store an auxiliary file next to your script, which contains the counter
verbatim.
$ python3 test.py
counter = 0
$ python3 test.py
counter = 1
$ python3 test.py
counter = 2
$ cat my_counter
3
edited Nov 20 at 8:36
answered Nov 20 at 8:29
MisterMiyagi
7,2252040
7,2252040
add a comment |
add a comment |
up vote
2
down vote
Use config parser to store run counter in a file
import configparser
config = configparser.ConfigParser()
config_fn = 'program.ini'
try:
config.read(config_fn)
run_counter = int(config.get('Main', 'run_counter'))
except configparser.NoSectionError:
run_counter = 0
config.add_section('Main')
config.set('Main', 'run_counter', str(run_counter))
with open(config_fn, 'w') as config_file:
config.write(config_file)
run_counter += 1
print("Run counter {}".format(run_counter))
config.set('Main', 'run_counter', str(run_counter))
with open(config_fn, 'w') as config_file:
config.write(config_file)
add a comment |
up vote
2
down vote
Use config parser to store run counter in a file
import configparser
config = configparser.ConfigParser()
config_fn = 'program.ini'
try:
config.read(config_fn)
run_counter = int(config.get('Main', 'run_counter'))
except configparser.NoSectionError:
run_counter = 0
config.add_section('Main')
config.set('Main', 'run_counter', str(run_counter))
with open(config_fn, 'w') as config_file:
config.write(config_file)
run_counter += 1
print("Run counter {}".format(run_counter))
config.set('Main', 'run_counter', str(run_counter))
with open(config_fn, 'w') as config_file:
config.write(config_file)
add a comment |
up vote
2
down vote
up vote
2
down vote
Use config parser to store run counter in a file
import configparser
config = configparser.ConfigParser()
config_fn = 'program.ini'
try:
config.read(config_fn)
run_counter = int(config.get('Main', 'run_counter'))
except configparser.NoSectionError:
run_counter = 0
config.add_section('Main')
config.set('Main', 'run_counter', str(run_counter))
with open(config_fn, 'w') as config_file:
config.write(config_file)
run_counter += 1
print("Run counter {}".format(run_counter))
config.set('Main', 'run_counter', str(run_counter))
with open(config_fn, 'w') as config_file:
config.write(config_file)
Use config parser to store run counter in a file
import configparser
config = configparser.ConfigParser()
config_fn = 'program.ini'
try:
config.read(config_fn)
run_counter = int(config.get('Main', 'run_counter'))
except configparser.NoSectionError:
run_counter = 0
config.add_section('Main')
config.set('Main', 'run_counter', str(run_counter))
with open(config_fn, 'w') as config_file:
config.write(config_file)
run_counter += 1
print("Run counter {}".format(run_counter))
config.set('Main', 'run_counter', str(run_counter))
with open(config_fn, 'w') as config_file:
config.write(config_file)
answered Nov 20 at 8:03
Pawel Stradowski
13918
13918
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388362%2fchange-hardcoded-values-in-python-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
One option would be to open in write mode your .py file at the end of your script, find the line you want to modify and replace the value.
– Patol75
Nov 20 at 7:49
2
Possible, of course - most things are. Desirable? Not really. If this value needs to persist across runs, then look at storing it in a file which you can load from your script each time.
– SiHa
Nov 20 at 7:50
Sure they are possible, but is there an easy way? I would like the values to be dynamic , like that 1 become 2. Let me rephrase it
– Durian Jaykin
Nov 20 at 7:51