createfile函數用于創建新文件,并返回一個指向該文件的文件對象。它的用法可以有以下幾種:
file = open(“filename.txt”, “w”)
file.close()
file = open(“filename.txt”, “w”)
file.write(“This is the content of the file.”)
file.close()
with open(“oldfile.txt”, “r”) as old_file:
with open(“newfile.txt”, “w”) as new_file:
new_file.write(old_file.read())
注意:在使用createfile函數時,要確保傳遞給函數的文件名是有效的并且不存在重名的文件。否則,函數可能會引發文件已存在或無效文件名的異常。