H
H
hardwellZero2015-05-19 08:59:49
Python
hardwellZero, 2015-05-19 08:59:49

The variable is on the way. How?

Good morning Toaster ;)
I have this little code:

import shutil
shutil.move("/home/hardwellzero/files/здесь_нужна_переменная", "/home/hardwellzero/здесь_тоже/и_здесь")

I have variable values. You need to force the files to be moved along this path. Tried using %s, didn't help. Are there any other options?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
anelyubin, 2015-05-19
@hardwellZero

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?

R
Roman, 2015-05-19
@idap

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)

B
bromzh, 2015-05-19
@bromzh

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 question

Ask a Question

731 491 924 answers to any question