用python写一个简单的去除源程序代码中行号的程序

        学习python没多久,想在网上下一些源码看看,但是由于很多代码都带有行号,要编译运行的时候一个个去删除不好弄,于是便萌生写一个这样的程序的想法,代码如下,这个程序还有很多不足的地方,希望日后能加以改进:

#!/usr/bin/python
filename=input("please imput the path of the file:")
f=open(filename,'r')
f1=open('target.py','w')
line=[]
line=f.readlines()
#while len(line)!=0:
s=''
for iline in line:
        k=0
        for i in iline:
                if i.isdigit() and int(i)>=0 and int(i)<=9:
                        k+=1
        s=iline[k+1:len(line)-1]
        f1.write(s)
f.close()
f1.close()
下面附上网上搜集到的关于python文件操作的资料以及这次练习的几点心得:

<1>

python 操作文件----文件读写

python进行文件读写的函数是open或file

file_handler = open(filename,,mode)

Table mode

模式

描述

r

以读方式打开文件,可读取文件信息。

w

以写方式打开文件,可向文件写入信息。如文件存在,则清空该文件,再写入新内容

a

以追加模式打开文件(即一打开文件,文件指针自动移到文件末尾),如果文件不存在则创建

r+

以读写方式打开文件,可对文件进行读和写操作。

w+

消除文件内容,然后以读写方式打开文件。

a+

以读写方式打开文件,并把文件指针移到文件尾。

b

以二进制模式打开文件,而不是以文本模式。该模式只对Windows或Dos有效,类Unix的文件是用二进制模式进行操作的。


Table 文件对象方法

方法

描述

f.close()

关闭文件,记住用open()打开文件后一定要记得关闭它,否则会占用系统的可打开文件句柄数。

f.fileno()

获得文件描述符,是一个数字

f.flush()

刷新输出缓存

f.isatty()

如果文件是一个交互终端,则返回True,否则返回False。

f.read([count])

读出文件,如果有count,则读出count个字节。

f.readline()

读出一行信息。

f.readlines()

读出所有行,也就是读出整个文件的信息。

f.seek(offset[,where])

把文件指针移动到相对于where的offset位置。where为0表示文件开始处,这是默认值 ;1表示当前位置;2表示文件结尾。

f.tell()

获得文件指针位置。

f.truncate([size])

截取文件,使文件的大小为size。

f.write(string)

把string字符串写入文件。

f.writelines(list)

把list中的字符串一行一行地写入文件,是连续写入文件,没有换行。


示例文件如下:

#-*- encoding:UTF-8 -*-
filehandler = open('c:\\111.txt','r')    #以读方式打开文件,rb为二进制方式(如图片或可执行文件等)

print 'read() function:'              #读取整个文件
print filehandler.read()

print 'readline() function:'          #返回文件头,读取一行
filehandler.seek(0)
print filehandler.readline()

print 'readlines() function:'         #返回文件头,返回所有行的列表
filehandler.seek(0)
print filehandler.readlines()

print 'list all lines'                #返回文件头,显示所有行
filehandler.seek(0)
textlist = filehandler.readlines()
for line in textlist:
    print line,
print 
print

print 'seek(15) function'               #移位到第15个字符,从16个字符开始显示余下内容
filehandler.seek(15)
print 'tell() function'
print filehandler.tell()              #显示当前位置
print filehandler.read()

filehandler.close()                   #关闭文件句柄



<2>

1.int()函数的参数只能是字符串形式,即int(' ')和int(‘a’)之类的均会出错

下面是python系统帮助里面对int函数的解释:

>>> help(int)  
Help on class int in module __builtin__:  
class int(object)  
 |  int(x[, base]) -> integer  
 |    
 |  Convert a string or number to an integer, if possible.  A floating point  
 |  argument will be truncated towards zero (this does not include a string  
 |  representation of a floating point number!)  When converting a string, use  
 |  the optional base.  It is an error to supply a base when converting a  
 |  non-string.  If base is zero, the proper base is guessed based on the  
 |  string content.  If the argument is outside the integer range a  
 |  long object will be returned instead.  
2.readlines()函数默认将所有读取到的数据以行为单位存放在一个列表里

3.write()函数不会自动加换行号

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值