嗨,我是python編程新手。
我想從源文件複製到目的地。我正在使用shutil.copy2(src,dst)。 但是在src和dst路徑中,我想使用變量。
例如(變量名):程序包名稱= XYZ_1000 這樣的src路徑將是:/家庭/數據/ $程序包名稱 /file.zip
在外殼我們可以用$程序包名稱訪問變量,所以Python中有沒有類似的方法?
主要關心的是,如果我想在複製命令中使用變量,我如何在python中實現這一點? 在此先感謝。
嗨,我是python編程新手。
我想從源文件複製到目的地。我正在使用shutil.copy2(src,dst)。 但是在src和dst路徑中,我想使用變量。
例如(變量名):程序包名稱= XYZ_1000 這樣的src路徑將是:/家庭/數據/ $程序包名稱 /file.zip
在外殼我們可以用$程序包名稱訪問變量,所以Python中有沒有類似的方法?
主要關心的是,如果我想在複製命令中使用變量,我如何在python中實現這一點? 在此先感謝。
pkg_name = XYZ_1000
使用格式()
src_path = "/home/data/{pkg_name}/file.zip".format(pkg_name=pkg_name)
OR
src_path = "/home/data/%s/file.zip" % pkg_name
OR
src_path = "/home/data/" + pkg_name + "/file.zip"
OR
src_path = string.Template("/home/data/$pkg_name/file.zip").substitute(locals())
# Or maybe globals() instead of locals(), depending on where pkg_name is defined.
http://stackoverflow.com/questions/4450592/string-interpolation-in-python – Blorgbeard
謝謝。但我的問題是不同的。讓我知道如何在python中使用copy命令中的變量? – deepu
看起來是一樣的,給出你接受的答案。 – Blorgbeard