import os
string = "mkdir P&C_directory && pwd'
os.system(string)
外殼結果是:
string = "mkdir P&d_directory"
In [11]: os.system(string)
sh: 1: d_directory: not found
如何將字串中的任何特殊字符替換為“\special_character”
uj5u.com熱心網友回復:
該&字符保留用于特殊目的(https://www.cyberciti.biz/faq/linuxunix-rules-for-naming-file-and-directory-names/)。最好不要將這些保留字符用于 Linux 和 Unix 檔案系統。但是,如果您確實需要使用它,或者如果您想在 Linux 檔案系統(ext2 / ext3 / etx4)以外的檔案系統上使用它,當然可以這樣做。
這可以使用引號來完成:mkdir '/tmp/test&test'甚至使用轉義字符:mkdir /tmp/test\&test。
root@vusolose:~# python
Python 2.7.16 (default, May 4 2021, 14:15:48)
[GCC 9.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> cmd1="mkdir '/tmp/test&test' && ls /tmp"
>>> os.system(cmd1)
test&test
0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/440892.html
標籤:python-3.x linux 重击 贝壳 操作系统
