小甲鱼python学习——文件

一:操作表格

1 .在这里插入图片描述
2.在这里插入图片描述

二:实例操作。

  1. 三种打开文件操作。
>>> f1 = open('F:/test.txt','w')
>>> f1
<_io.TextIOWrapper name='F:/test.txt' mode='w' encoding='cp936'>
>>> f1.close ()
>>> 
>>> f1 = open('F://test.txt','w')
>>> f1.close ()
>>> 
>>> f1 = open('F:\\test.txt','w')
>>> f1.close ()
  1. 一些示例操作。
>>>f = open('F:\\img.txt')
>>> 
>>> f.read()
"import tkinter as tk\nimport tkinter.filedialog \nimport cv2\nimport numpy as np\n##from matplotlib import pyplot as plt\n\n##制作窗口\nwindow = tk.Tk()\nwindow.title('My Window')\nwindow.geometry('640x480')\nlabel = tk.Label(window,text = '图像处理', bg = 'blue',font = ('楷体',12),width = 30)\nlabel.pack()\n\n## 解决中文路径函数问题\ndef cv_imread(file_path):\n    cv_img = cv2.imdecode(np.fromfile(file_path, dtype = np.uint8),-1)\n    return  cv_img\n\n## 按键功能函数\ndef selectpath():\n    file_path = tkinter.filedialog.askopenfilename()\n    global img\n    img = cv_imread(file_path)\n    cv2.namedWindow('image',0)\n   ## cv2.resizeWindow('image',700,600)\n    cv2.imshow('image',img)\n    k = cv2.waitKey(0)\n    cv2.destroyAllWindows()\n    \ndef rotate():\n    imginfo = img.shape\n    height = imginfo[0]\n    width = imginfo[1]\n    matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5) #mat rotate 1 center 2 angle 3 sc\n    dst = cv2.warpAffine(img,matRotate,(height,width))\n    cv2.namedWindow('dst',0)\n    cv2.imshow('dst',dst)\n    cv2.waitKey(0)\n    cv2.destroyAllWindows()\n       \n## 制作按键\nbutton1 = tk.Button(window,text = '打开图片', bg='yellow', font =('楷体',12),width = 10,height = 1,command = selectpath)\nbutton2 = tk.Button(window,text = '图片旋转', bg='yellow', font =('楷体',12),width = 10,height = 1,command = rotate)\nbutton1.place(x=80,y=100)\nbutton2.place(x=80,y=150)\n#button1.pack()\nwindow.mainloop()\n"
>>> f.read()             # 读完后,指针指向末尾
''
>>> f.seek(1,0)       # 重置指针
1
>>> f.read()
"mport tkinter as tk\nimport tkinter.filedialog \nimport cv2\nimport numpy as np\n##from matplotlib import pyplot as plt\n\n##制作窗口\nwindow = tk.Tk()\nwindow.title('My Window')\nwindow.geometry('640x480')\nlabel = tk.Label(window,text = '图像处理', bg = 'blue',font = ('楷体',12),width = 30)\nlabel.pack()\n\n## 解决中文路径函数问题\ndef cv_imread(file_path):\n    cv_img = cv2.imdecode(np.fromfile(file_path, dtype = np.uint8),-1)\n    return  cv_img\n\n## 按键功能函数\ndef selectpath():\n    file_path = tkinter.filedialog.askopenfilename()\n    global img\n    img = cv_imread(file_path)\n    cv2.namedWindow('image',0)\n   ## cv2.resizeWindow('image',700,600)\n    cv2.imshow('image',img)\n    k = cv2.waitKey(0)\n    cv2.destroyAllWindows()\n    \ndef rotate():\n    imginfo = img.shape\n    height = imginfo[0]\n    width = imginfo[1]\n    matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5) #mat rotate 1 center 2 angle 3 sc\n    dst = cv2.warpAffine(img,matRotate,(height,width))\n    cv2.namedWindow('dst',0)\n    cv2.imshow('dst',dst)\n    cv2.waitKey(0)\n    cv2.destroyAllWindows()\n       \n## 制作按键\nbutton1 = tk.Button(window,text = '打开图片', bg='yellow', font =('楷体',12),width = 10,height = 1,command = selectpath)\nbutton2 = tk.Button(window,text = '图片旋转', bg='yellow', font =('楷体',12),width = 10,height = 1,command = rotate)\nbutton1.place(x=80,y=100)\nbutton2.place(x=80,y=150)\n#button1.pack()\nwindow.mainloop()\n"
>>> f.tell()      #指针位置
1466
>>>list(f)     #变成列表
['import tkinter as tk\n', 'import tkinter.filedialog \n', 'import cv2\n', 'import numpy as np\n', '##from matplotlib import pyplot as plt\n', '\n', '##制作窗口\n', 'window = tk.Tk()\n', "window.title('My Window')\n", "window.geometry('640x480')\n", "label = tk.Label(window,text = '图像处理', bg = 'blue',font = ('楷体',12),width = 30)\n", 'label.pack()\n', '\n', '## 解决中文路径函数问题\n', 'def cv_imread(file_path):\n', '    cv_img = cv2.imdecode(np.fromfile(file_path, dtype = np.uint8),-1)\n', '    return  cv_img\n', '\n', '## 按键功能函数\n', 'def selectpath():\n', '    file_path = tkinter.filedialog.askopenfilename()\n', '    global img\n', '    img = cv_imread(file_path)\n', "    cv2.namedWindow('image',0)\n", "   ## cv2.resizeWindow('image',700,600)\n", "    cv2.imshow('image',img)\n", '    k = cv2.waitKey(0)\n', '    cv2.destroyAllWindows()\n', '    \n', 'def rotate():\n', '    imginfo = img.shape\n', '    height = imginfo[0]\n', '    width = imginfo[1]\n', '    matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5) #mat rotate 1 center 2 angle 3 sc\n', '    dst = cv2.warpAffine(img,matRotate,(height,width))\n', "    cv2.namedWindow('dst',0)\n", "    cv2.imshow('dst',dst)\n", '    cv2.waitKey(0)\n', '    cv2.destroyAllWindows()\n', '       \n', '## 制作按键\n', "button1 = tk.Button(window,text = '打开图片', bg='yellow', font =('楷体',12),width = 10,height = 1,command = selectpath)\n", "button2 = tk.Button(window,text = '图片旋转', bg='yellow', font =('楷体',12),width = 10,height = 1,command = rotate)\n", 'button1.place(x=80,y=100)\n', 'button2.place(x=80,y=150)\n', '#button1.pack()\n', 'window.mainloop()\n']
>>> f.seek(0)
0
>>> for each in f:
	print(each)               #文件对象迭代出来

	
import tkinter as tk

import tkinter.filedialog 

import cv2

import numpy as np

。。。。。。。。。。。。。。。。。。。。后面省略。。。。。。。。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值