第一种解法
程序代码如下
content = '诚挚邀请您来参加本次宴会。'
with open('D:\邀请函.txt','w') as fp:
fp.write(content)
f = open(r'D:\邀请函.txt',"a+")
data = f.write('诚挚邀请您来参加本次宴会')
f.close()
第一种解法
程序代码如下
content = '\nbest regards\n李雷'
with open('D:\邀请函.txt','a') as fp:
fp.write(content)
第二种解法
程序代码如下
f = open('D:\邀请函.txt',"a+")
data = f.write('诚挚邀请您来参加本次宴会')
f.write("\nbest regards\n")
f.write("李雷")
f.close()
第一种解法
程序代码如下
with open('D:\乘法表.txt',mode = 'w',encoding = 'utf-8') as fp:
for i in range(1,10):
for j in range(1,i+1):
fp.write('{} X {} = {} '.format(i,j,i*j))
fp.write('\n')
第二种解法
程序代码如下
with open("D:\乘法表.txt",mode = 'w+',encoding = 'utf-8') as f:
for i in range(1,10):
for j in range(1,i+1):
file = "{0} * {1} = {2}\t".format(i,j,i*j)
f.write(file)
f.write("\n")
第一种解法
程序代码如下
from openpyxl import Workbook
def main(txtFileName):
#得到对应的Excel文件名
new_XlsxFileName = txtFileName[:-3] + 'xlsx'
#创建工作簿,并获取其中第一个工作表
wb = Workbook()
ws = wb.worksheets[0]
#打开原始的记事本文件,依次读取每行内容,切分后写入Excel文件
with open(txtFileName) as fp:
for line in fp:
#切分后得到列表,可以直接追加到工作表中
#每个元素写入一个单元格
line = line.strip().split(',')
ws.append(line)
#保存Excel文件
wb.save(new_XlsxFileName)
main('D:\\test.txt')
第二种解法
程序代码如下
from openpyxl import Workbook
def main(txtFileName):
new_XlsxFileName = txtFileName[:-3] + 'xlsx'
wb = Workbook()
ws = wb.worksheetx[0]
with open(txtFileName,mode = 'r',encoding = 'uttf-8') as fp:
for line in fp:
line = line.strip().split(',')
ws.append(line)
wb.save(new_XlsxFileName)
main(r'D:\text.txt')
第一种解法
程序代码如下
from os import listdir
from os.path import join,isdir
def find(directory,fileName):
all_dir = [directory]
#print(all_dir)
while(all_dir):
now = all_dir.pop(0)
#print(now)
#print(listdir(now))
for subPath in listdir(now):
if subPath == fileName:
return '该文件夹下存在temp.txt的文件'
path = join(now,subPath)
if isdir(path):
all_dir.append(path)
return '该文件夹下不存在temp.txt的文件'
print(find('D:\\1','excel.txt'))
第二种解法
程序代码如下
from os import listdir
from os.path import join,isdir
def search(directory,fileName):
dirs = [directory]
while dirs:
current = dirs.pop(0)
print(current)
for subPath in listdir(current):
if subPath == fileName:
return '存在文件'
path = join(current,subPath)
if isdir(path):
dirs.append(path)
print(search(r'D:','temp.txt'))