簡體   English   中英

需要從while循環的輸出中創建新文件

[英]Need to create new file from the output of while loop

我正在嘗試從 while 循環的輸出創建新文件。 文件名包含 1 2 3

import subprocess
f = open("/path/filename", "r")
while True:
    line = f.readline()
    print(line) 

    if not line:
    break

上面的代碼片段將打印為:1, 2, 3

現在我的問題是,我需要創建名稱為 1,2,3(while 循環的輸出)的新文件/目錄。 我需要子流程模塊來做到這一點。 請建議我。

嘗試這個

import subprocess

f = open("test.txt", "r")
files_list = f.readlines()
files = ", ".join(files_list).replace("\n","")
cmd = "touch {0}".format(files)
subprocess.call(cmd, shell=True)

試試這個

import subprocess

f = open("/path/filename", "r")
while True:
    line = f.readline()
    print(line) 

    subprocess.call('touch {}'.format(line), shell=True) 

    if not line:
        break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM