在 变量 名前加上
$。
例
假设您要将文件复制
file1到存储在名为的python变量中的路径中
dir_pth:
dir_path = "/home/foo/bar"!cp file1 $dir_path
从Ipython或Jupyter笔记本中
编辑
感谢Catbuilts的建议,如果您想连接多个字符串以形成路径,请使用
{..}代替$..$。在两种情况下都有效的一般解决方案是坚持
{..}dir_path = "/home/foo/bar"!cp file1 {dir_path}如果要
sub_dir在路径中添加另一个字符串,则:
!cp file1 {dir_path + sub_dir}编辑2
有关使用原始字符串(以前缀
r)传递变量的相关讨论,请参阅将Ipython变量作为字符串参数传递给shell命令



