import numpy as np
import os,sys

#获取当前文件夹,并根据文件名
def path(fileName):
    p=sys.path[0]+'\\'+fileName
    return p

#读文件    
def readFile(fileName):
    f=open(path(fileName))
    str=f.read()
    f.close()
    return str
 
#写文件 
def writeFile(fileName,str):
    f=open(path(fileName),'w')
    f.write(str)
    f.close()

def str1():
    str=','.join('我在中国大地上骄傲地生长着!')
    return str

def str2():
    return str(np.random.randint(-49,50,[3,3,3]))

#实验1    
def test_1():
    fileName='中国大地.txt'
    writeFile(fileName,str1())
    list=readFile(fileName).split(',')
    print(list)

#实验2
def test_2():
    writeFile('str1',str1())
    writeFile('str2',str2())
    str_1=readFile('str1')
    str_2=readFile('str2')
    print(str_1)
    print(str_2)
    
test_2()

 知识点讲解:python系列——文件操作

相关文章:

  • 2021-06-23
  • 2022-02-07
  • 2022-12-23
  • 2022-01-21
  • 2021-09-24
  • 2021-11-10
  • 2022-12-23
  • 2021-12-15
猜你喜欢
  • 2018-04-10
  • 2021-12-14
  • 2019-03-19
  • 2022-01-19
  • 2021-05-06
  • 2021-10-18
相关资源
相似解决方案