【python】实例-创建文件并通过键盘输入字符
import os
lnend=os.linesep ##windows行结束符号是“\r\n”
FileName=raw_input("please input filename:")
while True:##检查该文件是否存在,当执行到break时跳出while循环
if os.path.exists(FileName):##检索脚本所在目录的位置
print "%s already exits" %FileName
FileName=raw_input("please input filename:")
else:
break
ALL=[]##创建空的列表用于存储输入内容
print "please input words (ok to stop inputing)\n"
while True:##当执行到break时跳出while循环
words=raw_input(">>")
if words=="ok":
break
else:
ALL.append(words)##循环的往列表添加内容
FileHandle=open(FileName,"w")
FileHandle.writelines(["%s%s" %(x,lnend) for x in ALL] )##函数writelines(list)可以将list写入到文件中,但是不会在list每个元素后加换行符,因此需要lnend换行符。同样write(str)把str写到文件中,write()也不会在str后加上一个换行符
FileHandle.close()
print "DONE"
【python】实例-创建文件并通过键盘输入字符的更多相关文章
- python 批量创建文件及文件夹(文件夹里再创文件)
python 批量创建文件及文件夹(文件夹里再创文件)思路:文件建到哪>文件名字叫啥>创建文件夹>去新建的文件下>新建文件>给文件里边写东西>写个反馈给控制台> ...
- Python打印到屏幕_读取键盘输入
Python打印到屏幕_读取键盘输入: print( ): 打印输出括号中的值 print("hello") # hello strs = 'hello' print(" ...
- python 实现创建文件夹和创建日志文件
一.实现创建文件夹和日志 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author: nulige import os import datetime ...
- Ubuntu18.04系统中vi键盘输入字符不匹配
起因 今天重装了我的雷神笔记本的ubuntu18.04,不要问我为什么,我就是想复习下重装系统而已.好吧,我承认我改错文件启动不起来了. 于是我要重装jdk.maven and so on,但是当我用 ...
- python之创建文件写入内容
https://www.cnblogs.com/evablogs/p/7096686.html 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...
- python 批量创建文件
# coding:utf8 import os path = "D:/Python_mkfile" os.chdir(path)#切换到该目录 ysyl = u"验收文件 ...
- Python实例 复制文件
import shutil import os import os.path src = " d:\\download\\test\\myfile1.txt " dst = ...
- Python实例 遍历文件夹和文件
import os import os.path # os,os.path里包含大多数文件访问的函数,所以要先引入它们. # 请按照你的实际情况修改这个路径 rootdir = &quo ...
- python:创建文件
#!/usr/bin/python# -*- coding:utf-8 -*- open('a.txt','w+')
随机推荐
- Py打包exe报错
Py打包exe报错 下载地址 https://github.com/pyinstaller/pyinstaller 用管理员执行 pip install https:/ ...
- linux processes identifiers
Linux, like all Unix uses user and group identifiers to check for access rights to files and images ...
- python自动化运维之路06
python中面向对象编程 编程范式: 编程是 程序 员 用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程 , 一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条 ...
- bzoj1088 [SCOI2005]扫雷
题解: 首先枚举第一个有木有雷 然后第二个可以通过第一个推,第三个也是 以此类推 最后判断是否合法 代码: #include<bits/stdc++.h> using namespace ...
- 数据结构(C语言)关于树、二叉树、图的基本操作。
1) 编写算法函数int equal(tree t1, tree t2),判断两棵给定的树是否等价: int equal(tree t1,tree t2) { int k; if(t1==NULL&a ...
- 1.3 C++引用(Reference)
参考:http://www.weixueyuan.net/view/6328.html 总结: 引用是变量的另外一个别名,不是指针,与原变量名同指相同的内存.可以原变量的值. 在函数中作为形参可以修改 ...
- tp 缓存,前台提速
- np.stack() 与 tf.stack() 的简单理解
说明:np ----> numpy tf ----> tensorflownp.stack(arrays, axis=0) np.stack(arrays, axis=0) - ...
- Linux文件共享(单进程之间、多进程之间)
转载:https://www.cnblogs.com/frank-yxs/p/5925603.html 在同一个进程中,实现文件共享的方法有两种: 多次使用open函数打开相同文件 使用dup/dup ...
- ubuntu vsftpd
With a bit of playing around I've managed to come up with a semi solution (not perfect but good enou ...