This question already has answers here:
how can I disable multiple instances of a python script?
(7 answers)
Closed 2 years ago.
When I open my program I want it to detect if the program is running already (if I launch it again while it's running). If it is I want to access the already running program and call a function there. But the problem is that I don't know how to access the already running program.
Thanks in advance!
I am confused by what you mean by "the script is the same script", but you can use import to get a function from a different file
otherprogram.py
def func(a):
return a
main.py
from otherprogram import func
print(func(5))
Related
This question already has an answer here:
How can I set file creation times in ZFS?
(1 answer)
Closed 12 months ago.
utime only modifies accessed and modified times. Is there a way to do modify the creation/birth time with Python?
Filesystem is ZFS if that matters.
You want touch functionality, and it's available in the os module using utime().
See https://docs.python.org/3.8/library/os.html#os.utime, and also
Implement touch using Python?
This question already has answers here:
Passing IPython variables as arguments to bash commands
(4 answers)
Running Bash commands in Python
(11 answers)
Closed 2 years ago.
So I found this tool: https://github.com/elceef/dnstwist and I want to pass a list of domains into that tool then take the output and visualize it.
It operates through the command line, but how do I automate entering each domain and process the output automatically as well?
By the way I am using colab, so I would need a solution using jupyter notebooks!
Thanks!
If you want to do this programmatically I would not use the provided cli – although this would also be possible.
Instead you can create a small python script to do this.
Import dwintwist in your script and have a look at the main function in dwintwist.py to see how you call this.
Doesn't look to hard, but I don't know your programming skill level of course.
This question already has answers here:
How to get local variables updated, when using the `exec` call?
(3 answers)
Closed 4 years ago.
In my main script, I import one of my own module which contains global variables. This main script execute another script with the function exec (exec(compile(open(Seq_1, "rb").read(), Seq_1, 'exec')) and this other script import the same module.
So my question is: does these scripts have access to the same global variables (that means if I modify one global variable, the other script will be impacted) or not?
Python will run your file when you first import it. On the second import python won't re-run the file.
In practice, python functions and variables directly on modules (not wrapped in classes) works like singletons.
This answer explains more about it. You can directly refer to the docs, also suggested on linked answer.
This question already has answers here:
How do I call a function from another .py file? [duplicate]
(19 answers)
Closed 6 years ago.
I'm new to Python, coming from Matlab like many. I'm used to defining my functions as standalone .m files and calling them easily from a second script as long as the function is saved somewhere within the defined Matlab path.
I've learned how to define a (user-defined) function in Python (def my_function() etc.), but I'm coming up short in my Google searches on a way to define the function in a a separate .py file A, and how to call it in another script B. All help files I can find give support on how to define the function within the same script. When I try calling the function (which I've defined as a .py file in the same folder I'm using for script A) my script doesn't recognize it. Do I need to be declaring the function at the beginning of my script? I'm getting the sense that Python is very different in the way it handles these things than Matlab--perhaps I can't do this?
Cheers
You have to use imports
for example, you have the function doSomething() in functions.py and want to work in main.py
in main.py
from functions import doSomething
var = doSomething()
This question already has answers here:
How can I access environment variables in Python?
(16 answers)
Closed 9 years ago.
I'm new to Python and would like reproduce a convenience I used when working in Perl.
When calling a Perl script I usually set some $ENV variables (like VERBOSE, DEVELOP and DEBUG). Inside the called script I recover their values using
my $verbose=$ENV{VERBOSE};
my $develop=$ENV{DEVELOP};
my $debug=$ENV{DEBUG};
This allow print stmts conditional on these variables.
Can I do the same thing in Python? I know thanks to previous responses (Thank you!) to use os.environ[var] within the script to access the values. But I have not been able to figure out how to assign a value to a variable when I call the script from the command line as I could when callinbg a Perl script
Can values be set for such variables on the commandline invoking a python script?
TIA
They are accessible via the os.environ dictionary.
os.environ['VERBOSE']