1. 程式人生 > >python之創建文件寫入內容

python之創建文件寫入內容

class 重名 pen true adl blog open ont 數據

#!/usr/bin/python
#-*-conding-*-
#創建文件,並寫入數據:要求不能與現存系統文件重名

import os

def makefile(path,content):
    if os.path.exists(path):
        if os.path.isdir(path):
            f = open(‘makefile_test.txt‘,‘w+‘)
            f.write(content)
            f.seek(0)
            read = f.readline()
            f.close()
            print(read)
        else:
            print(‘please input the dir name‘)
    else:
        print(‘the path is not exists‘)


path = str(input(‘the path:‘))
content = str(input(‘please write the content into the new file:‘))
makefile(path,content)

python之創建文件寫入內容