共計 366 個字符,預計需要花費 1 分鐘才能閱讀完成。
你可以使用 Python 中的 open()
函數來創(chuàng)建文件。下面是一個示例:
import os
path = '/path/to/your/directory'
filename = 'example.txt'
# 拼接路徑和文件名
file_path = os.path.join(path, filename)
# 創(chuàng)建文件并寫入內容
with open(file_path, 'w') as file:
file.write('Hello, world!')
在上面的示例中,首先指定了要創(chuàng)建文件的路徑和文件名。然后使用 os.path.join()
函數將路徑和文件名拼接在一起,得到文件的完整路徑。最后使用 open()
函數以寫入模式(‘w’)打開文件,并寫入內容。
丸趣 TV 網 – 提供最優(yōu)質的資源集合!
正文完