简体   繁体   English

一个程序,它将使用python 3修改另一个程序

[英]A program which will modify another program by using python 3

I am trying to write a program in python which will modify another python program. 我试图用python编写程序,它将修改另一个python程序。 Steps will be as follows: 步骤如下:

  1. I have a program (pyh.py) which contains below-mentioned lines and output is as follows 我有一个包含以下各行的程序(pyh.py) ,输出如下

output: 输出:

Number of lines: 6 
Number of characters: 65

code for pyh.py : pyh.py代码:

import os
import sys

def pyh( filename ):

    if ( os.path.isfile( filename ) ):

        file = open( filename, 'r' )
        line_list = file.readlines()
        pyh_compute_size( line_list )
        file.close()

    else:
        print( "File " + filename + " does not exist." )


def pyh_compute_size( line_list ):

    line_count = 0; char_count = 0
    for line in line_list:

        line_count += 1
        char_count += len( line )

    print( "Number of lines: " + str( line_count ) )
    print( "Number of characters: " + str( char_count ) )


if __name__ == '__main__':

    pyh( "text_a.txt" )
  1. What I am trying to do is, I am writing another python program called ' modifier.py ' which will open ' pyh.py ' (in the same directory) and read the file then close it. 我正在尝试做的是,我正在编写另一个名为“ modifier.py ”的python程序,该程序将打开“ pyh.py ”(在同一目录中)并读取文件,然后将其关闭。
  2. Then open the same file ' pyh.py ' for writing. 然后打开相同的文件“ pyh.py ”进行写入。 It will go through the list of line and write line 1 to 20 in the new file ( modifier.py ). 它将遍历行列表,并将行1到20写入新文件( modifier.py )。 After that, it removes the newline from the end of it, and add; 之后,它从末尾删除换行符,然后添加; print ("Additional Part" ) to the end, and then add a newline. 在末尾print ("Additional Part" ),然后添加换行符。
  3. After that, it will write from 21 to end to the new file ( modifier.py ). 在那之后,它将从21写入到新文件( modifier.py )的结尾。

When I run the 'modifier.py' it will modify the 'pyh.py' (for line between 20 & 21). 当我运行'modifier.py' ,它将修改'pyh.py' (对于20和21之间的行)。 When 'pyh.py' is modified then it should show the below output. 修改'pyh.py'后,应显示以下输出。

Additional Part
Additional Part
Number of lines: 6 
Number of characters: 65

I am trying to do the step 3 & 4 but unable to find the appropriate way. 我正在尝试执行第3步和第4步,但是找不到合适的方法。 I am new in python. 我是python的新手。 So it would be really great if someone helps me to solve it. 因此,如果有人帮助我解决问题,那将是非常不错的。

code for modifier.py is as follow: modifier.py代码如下:

import os
import sys


def pyh_new( filename ):

    if ( os.path.isfile( filename ) ):

        file = open( filename, 'r' )
        line_list = file.readlines()
        file.close()

def pyh_new( filename ):

    if ( os.path.isfile( filename ) ):

        file = open( filename, 'w' )
        line_list = file.writelines()
        #need to write 

        file.close()



if __name__ == '__main__':

    pyh_new( "pyh.py" )

You have pyh_new listed twice. 您已列出pyh_new两次。 What you could do is call the name of the first one phy_new_read, and the second one phy_new_write. 您可以做的是调用第一个phy_new_read和第二个phy_new_write的名称。 Then you would want to make phy_new_read return the line_list. 然后,您需要使phy_new_read返回line_list。 return line_list . return line_list You would then be able to do: 然后,您将可以执行以下操作:

if __name__ == '__main__':

    lines = pyh_new_read( "pyh.py" )

    #grab the specific lines that you want. See 
    # https://stackoverflow.com/questions/2081836/reading-specific-lines-only-python

    pyh_new_write( lines )

See the following question for how to get specific lines from a file Reading specific lines only (Python) 有关如何从文件中获取特定行的信息,请参见以下问题: 仅读取特定行(Python)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM