『壹』 python如何根據輸入值動態創建文件目錄下再創建一個文件夾
import os
basepath = os.getcwd() # 如果你想在當前路徑下創建
# basepath = r"C:\\" #如果你想在特定路徑下創建
d1 = input("d1:")
full1 = os.path.join(basepath, d1)
os.makedirs(full1, exist_ok=True)
d2 = input("d2:")
full2 = os.path.join(d1, d2)
os.makedirs(full2, exist_ok=True)
『貳』 python 如何新建一個新的File
#python
f=open('f.txt','w') # r只讀,w可寫,a追加
for i in range(0,10):f.write(str(i)+' ')
例子:
#!/usr/bin/python
#coding=utf-8
import os
import time
import sys
f=open('a.txt','a')
f.write(os.popen('netstat -nltp | grep 22').read())
f.close()
關於上述創建文件,文件內容追加
#python
import random
f=open('f.txt','a')
for i in range(0,10):f.write(str(random.randint(0,9)))
. . .
f.write(' ')
f.close()
或者
#python
import rando
f=open('f.txt','a')
for i in range(0,10):
for i in range(0,10):f.write(str(random.randint(0,9)))
f.write('
')
f.close()
『叄』 求一個用python讀取文檔中的名字然後創建多個文件夾同時以創建文件夾名字命名的一個txt文件
代碼如下,就是這么簡單:
importos
basepath='c:data';
forlineinopen('a.txt'):
basename=line.strip()
folder=os.path.join(basepath,basename)
filename=os.path.join(folder,basename)
os.mkdir(folder)
open(filename,'w').close()
望採納,謝謝支持!
『肆』 如何在mac上用python創建txt文件並寫入內容
難道WIN/MAC下的語法跟Unix/linux下的語法差別這么大?I don't know......
linux下是這么寫的
logfile = open('hhh.txt','w')
logfile.write('drummer')
logfile.close()
『伍』 如何在python中建立文件
創建一個文本文檔(.txt),保存的時候將文件格式輸入.py,如圖:
相關推薦:《Python教程》
也可以在python環境下使用fwrite等語句進行編輯,或者使用自帶的IDLE編輯器編輯(右鍵選擇即可)
可下載notepad等編輯軟體,支持多種語言創建、編輯,在保存時選擇.py進行保存即可。
『陸』 python創建根據時間的txt文件
importtime
tm=time.strftime("%Y-%m-%d%X",time.localtime())
timeslog=tm+r'.txt'
sp=open(timeslog,'w')
sp.close()
代碼沒問題
但是Windows系統中 文件名不能包含下列任何字元:
/ : * ? 」(英文右引號) < > |
所以考慮下修改下時間格式
『柒』 請教各位如何用python創建文件和文件夾
python創建文件
>>>f=open('f.txt','w') # r只讀,w可寫,a追加
>>>for i in range(0,10):f.write(str(i)+'\n')
>>> f.close()
python創建文件夾
import os
os.makedirs("目錄")
如果解決了您的問題請採納!
如果未解決請繼續追問!