首页 > 编程语言 >【笨方法学python】ex17 - 更多文件操作

【笨方法学python】ex17 - 更多文件操作

时间:2022-10-05 07:11:30浏览次数:51  
标签:exists python indata ex17 file print out 方法学

代码如下:

点击查看代码
# -*-coding:utf-8- -*-
# 更多文件操作

from sys import argv
from os.path import exists
# exists 模块: 将文件名字符串作为参数,如果文件存在,返回 True
# 如果文件不存在,返回 False

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

# we could do these tow on one line too, how?
in_file = open(from_file)
indata = in_file.read()

print "The input file is %d bytes long" % len(indata)

print "Dose the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

out_file = open(to_file, 'w')
out_file.write(indata)

print "Alright, all done."

out_file.close()
in_file.close()


新建文档 "ex17-1"

点击查看代码
123456

执行结果:
image

标签:exists,python,indata,ex17,file,print,out,方法学
From: https://www.cnblogs.com/TiramisuPS/p/16754991.html

相关文章

  • 【笨方法学python】ex16 - 读写文件
    代码如下:点击查看代码#-*-coding:utf-8--*-#读写文件#close-关闭文件(保存)。#read-读取文件内容,结果可赋值给一个变量。#readline-读取文本文件中的一......
  • python的入门TCP编程
    一、创建TCP服务器创建TCP服务器的流程:使用socket创建一个套接字使用bind绑定IP和端口使用listen让套接字变成可以被动连接使用accept等待客户端的连接使用recv/send接收......
  • 【笨方法学python】ex15 - 读取文件
    代码如下:点击查看代码#-*-coding:utf-8--*-#读取文件fromsysimportargvscript,filename=argvtxt=open(filename)print"Here'syourfile%r:"%f......
  • 【笨方法学python】ex13 - 参数、解包、变量
    代码如下:点击查看代码#-*-coding:utf-8--*-#参数、解包、变量fromsysimportargv#argv是所谓的“参数变量”,是一个非常标准的编程术语。#包含了你传递给......
  • 【笨方法学python】ex14 - 提示和传递
    代码如下:点击查看代码#-*-coding:utf-8--*-#提示和传递fromsysimportargvscript,user_name=argvprompt='>'print"Hi%s,I'mthe%sscript."%(u......
  • 【笨方法学python】ex11 - 提问
    代码如下:点击查看代码#-*-coding:utf-8--*-#提问print"Howoldareyou?",age=raw_input()print"Howtallareyou?",height=raw_input()print"Howmu......
  • 【笨方法学python】ex12 - 提升别人
    代码如下:点击查看代码#-*-coding:utf-8--*-#提示别人age=raw_input("Howoldareyou?")height=raw_input("Howtallareyou?")weight=raw_input("Howm......
  • 【笨方法学python】ex10 - 那是什么
    代码如下:点击查看代码#-*-coding:utf-8--*-#那是什么tabby_cat="\tI'mtabbedin."persian_cat="I'msplit\nonaline."Backslash_cat="I'm\\a\\ca......
  • 【笨方法学python】ex3 - 运算符
    代码如下:点击查看代码#-*-coding:utf-8--*-print"中文示例"#+plus加号#-minus减号#/slash÷#*asterisk×#%percent余数#<less-than小于#......
  • 【笨方法学python】ex4 - 变量和命名
    代码如下:点击查看代码#-*-coding:utf-8--*-#变量和命名cars=100#100辆车space_in_a_car=4#每辆车四个位置drivers=30#30个司机passengers=90#......