2015-05-29 79 views
0

我打算打開多個文件(一個接一個,在bash終端中使用for循環),並使用PLINK(一個程序)和稍後的python函數修改它。以下是代碼:python函數與for循環在bash終端

for i in {1..10}; do 
    plink --cow --noweb --lfile $i --extract extract1.snp --recode --out 1$i 
    python -c 'import file_convert;file_convert.convert_tree_mix("1$i.map","tmp$i")' 
done 

但是,正如預期,蟒蛇無法讀取,無法打開「11.map」,它並沒有取代「$ I」與1.如何修改代碼,以便蟒蛇功能,與組合f​​or循環,打開不同的文件基礎上的價值每一次「我」

+0

嘗試'蟒蛇-c 「進口file_convert; file_convert.convert_tree_mix(\」 $ 1 i.map \ 「\ 」TMP $ I \「)」' –

+0

非常感謝你,它的工作:) –

回答

0

您需要在雙引號內包含整個python代碼,以便Python代碼中的$1將展開。 shell中的$1指的是第一個參數。

python -c "import file_convert;file_convert.convert_tree_mix(\"1$i.map\",\"tmp$i\")" 
3

你有沒有打過電話蟒蛇那樣:

python -c 'import sys; import file_convert;file_convert.convert_tree_mix(sys.argv[1],sys.argv[2])' "1$i.map" "tmp$i"; 

+1

一般來說,這比嘗試使用shell插值爲Python執行構建動態字符串要好。 – chepner