2017-08-29 31 views
-1

創建文件在Linux上,我想它的名字與Python創建一個PID文件,我知道是bash可能是這樣的:與PPID

outfile=/tmp/saul.$$.tmp 

,然後我可以只重定向輸出變量:

echo "Hello World" >> $outfile 

我在python試過這樣:

log="/home/ss055g/log_$$.log" 
f=open(log,'w') 
f.write("Esto es una prueba") 
f.close() 

,並沒有工作。它創建的日誌是:log _ $$。log,我的問題是,如何將pid添加到日誌而不是$$。

在此先感謝

回答

-2

這應該適合你。

import os 
pid = os.getpid() 
log_file="/home/ss055g/log_%s.log" % pid 
f=open(log_file,'w') 
f.write("Esto es una prueba") 
f.close() 
相關問題