我有一個名為test's file.txt內部test's dir目錄的檔案。所以檔案路徑變為test's dir/test's file.txt. 我想對檔案的內容進行分類,但由于該檔案包含一個撇號和一個空格,所以我很難做到這一點。我嘗試了幾個命令,包括
sh -c "cat 'test's dir/test's file.txt'"sh -c 'cat "test's dir/test's file.txt"'sh -c "cat '"'"'test's dir/test's file.txt'"'"'"sh -c 'cat "test\'s\ dir/test\'s\ file.txt"'還有更多......但它們都沒有作業。
一些幫助將不勝感激。謝謝!
uj5u.com熱心網友回復:
請你試試:
sh -c "cat 'test'\''s dir/test'\''s file.txt'"
至于路徑名部分,它是以下內容的串聯:
'test'
\'
's dir/test'
\'
's file.txt'
[編輯]
如果您想在 中執行 shell 命令python,請嘗試:
#!/usr/bin/python3
import subprocess
path="test's dir/test's file.txt"
subprocess.run(['cat', path])
或立即:
subprocess.run(['cat', "test's dir/test's file.txt"])
由于該subprocess.run()函式將命令作為串列,而不是單個字串(可能帶有shell=True選項),我們不必擔心命令周圍的額外參考。
請注意subprocess.run()Python 3.5 或更新版本支持。
uj5u.com熱心網友回復:
您可以使用此處的檔案:
sh -s <<-'EOF'
cat "test's dir/test's file.txt"
EOF
uj5u.com熱心網友回復:
此選項避免了對兩個級別的參考的需要:
sh -c 'cat -- "$0"' "test's dir/test's file.txt"
請參閱如何使用“bash -c”命令使用位置引數?. (它也適用于sh -c。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/436278.html
下一篇:檔案夾大小作為變數
