This question already has answers here:
How to evaluate environment variables into a string in Python?
(3 answers)
Closed 2 years ago.
I have the following string :
some_string = "%envvar1%\location\execution.exe"
envvar1 is an environment variable with value of "c:\", and I would like some function as following:
some_string = "%envvar1%\location\execution.exe"
inject_env_variable(some_string)
print(some_string)
"c:\location\execution.exe"
Creating a function like this wouldnt be to difficult with regular expressions and os.environ but I was wondering if there was some kind of built in module that treats these kind of things.
Note: google searching anything with the word 'path' and 'python' is really tedious since all the searches are related to pythonpath :P
os.path.expandvars is probably what you are looking for. https://docs.python.org/3/library/os.path.html#os.path.expandvars
import os
def inject_env_variable(s):
return s.replace("%envvar1%", os.environ['envvar1'])
Should do the trick
Related
This question already has answers here:
How to convert string to variable name?
(3 answers)
Closed 1 year ago.
I want to run a function of a module with a variable name for example:
Plugins.SomeUnknownName.function() without knowing their name but Plugins. and .function() stay the same.
I have tried several things like plugin = 'Plugins.' + unknown_plugin_name + '.function()' or
plugin_name = "SomePlugin"
Plugins.plugin_name.function()
But I don't know any further.
Maybe someone knows how to solve that.
Also this is my first stack overflow post, so please correct me if I did something wrong.
You can use getattr to pass the string name. Using numpy as an example here's what the normal usage would look like
>>> import numpy
>>> numpy.random.randint(1, 5)
2
Now similar to your example
>>> getattr(numpy, 'random').randint(1, 5)
2
So applying to your case
getattr(Plugins, 'plugin_name').function()
This question already has answers here:
How do you import a file in python with spaces in the name?
(4 answers)
Closed 1 year ago.
When I'm importing packages/custom modules the import statement only accepts single word input. I looked up the Packages info in the Python Docs and the example there only has one word imports. Are multi-word imports possible?
This works:
import Histogram
But this doesn't:
import File existence check
You could use the builtin __import__, but it'd probably be better to not have spaces in the name.
package = __import__("Name with spaces")
Don't use space when you want to name something. Use Camelcase like FileExistenceCheck or go with File_existence_check. I don't know if it would work just seen from the syntax but anyway you should not do it!
This question already has answers here:
Python efficient way to check if very large string contains a substring
(8 answers)
Closed 1 year ago.
I'm a beginner to Python. I'm using the request module to get the text from a website that contains blacklisted users for the login system of my program. I want to know how to check if a variable appears in another variable such as, "if variable appears in variable2: do something"
Can anyone help? Thanks.
You can check that using the in keyword -
if object1 in object2:
#do something
Share your code. It would give a better understanding of what you need to do. I think the below code will work.
import requests
x = requests.get('https://yourwebsite.com')
if variable in x.text:
#do something
This question already has answers here:
Python Named Argument is Keyword?
(2 answers)
Closed 6 years ago.
I'm writing a python script wherein I need to use the continue word.
I know that it is a python keyword.
So how should I write my script so that python will not complain that I am using a keyword as a string literal.
Thanks in advance for your time.
You should use something like:
_continue
But why not using more descriptive and longer variable name?! like:
continue_whatever or go_on ...
The normal practice is to use a trailing underscore to use a keyword that already being used by the language, like continue_ or input_.
I don't think anyone would think that just overwriting keywords is every a good idea.
This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
I am new to python and trying to understand the _full_path from this example.
def _full_path(self, partial):
if partial.startswith("/"):
partial = partial[1:]
path = os.path.join(self.root, partial)
return path
What does the function do? Specifically, what does this line do?
partial = partial[1:]
It seems like some kind of list manipulation -- but I can't find syntax like that in this document.
What is the root property of self that is getting called?
Can somebody explain a little bit about what is happening in that code.
Because os.path.join will take later path start with '/' as base, try this:
print os.path.join('/a', '/b/')
it return '/b/', so you have to check and remove begin slash when you join path.
str is a sequence type, check here: http://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange
That line drops the starting "/".
The function itself gives back the "full path".