Answer the question
In order to leave comments, you need to log in
The variable is on the way. How?
Good morning Toaster ;)
I have this little code:
import shutil
shutil.move("/home/hardwellzero/files/здесь_нужна_переменная", "/home/hardwellzero/здесь_тоже/и_здесь")
Answer the question
In order to leave comments, you need to log in
why not do something like:
import shutil
path1 = "/home/hardwellzero/files/" + str(variable_1)
path2 = ""/home/hardwellzero/" + str(variable_2) + "/" + str( variable_3)
shutil.move(path1, path2)
Or is that not good?
Another cross-platform option
import shutil
import os
path_1 = os.path.join("/home/hardwellzero/files", var_1)
path_2 = os.path.join("/home/hardwellzero", var_2, var_3)
shutil.move(path1, path2)
If you need exactly the path, then use, as already said os.path.join
, preferably always.
If you just insert the value of the variable(s) into a string, then use interpolation. There are 2 options: with a percent sign or via a method format
:
a = 1
b = 'foo'
c = [1, 2, 3]
s1 = 'string with vars: %s %s %s' % (a, b, c)
s2 = 'string with vars: {} {} {}'.format(a, b, c)
s2 = 'string with vars: {2} {0} {1}'.format(a, b, c)
s2 = 'string with vars: {foo} {bar} {qux}'.format(foo=a, qux=b, bar=c)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question