简体   繁体   English

如何将普通的 function 与另一个 function 结合起来,将不同的文本文件合并到一个文件中?

[英]How to combine a normal function with another function that will combine different text files into one file?

I want to combine out_fun() to combinefiles().我想将 out_fun() 与 combinefiles() 结合起来。 Currently I have it where out_fun() it writes to file3.txt and then closes it.目前我有它 out_fun() 它写入 file3.txt 然后关闭它。 Next, I have it where out_fun() is called within combinefiles() to write "Hello World" in order to combine out_fun() with combinefiles().接下来,我在 combinefiles() 中调用 out_fun() 来编写“Hello World”,以便将 out_fun() 与 combinefiles() 结合起来。 This solution is not working because it ends up printing hello world three separate times in the Python Shell and just says "None" within the file3.txt file.该解决方案不起作用,因为它最终在 Python Shell 中分别打印了三个不同的时间,并且在 file3.txt 文件中只显示“无”。 I'm wondering how I can do this so it doesn't make this so it doesn't print "Hello World" three times as I want it only to be printed once like in out_fun() and then I want "Hello World" added to the bottom of combinefiles().我想知道我该怎么做才能做到这一点,所以它不会打印“Hello World”三次,因为我希望它只打印一次,就像在 out_fun() 中一样,然后我想要“Hello World”添加到 combinefiles() 的底部。 I cannot use return because it gives a syntax error at 'end=""'我不能使用 return,因为它在 'end=""' 处给出了语法错误

def out_fun(): 
    print("Your number is ", end="")
    print(random.randint(0,5)
output = out_fun() 
file = open("file3.txt","w") 
file.write(str(output))
file.close() 

def file1():
    fileone = input("Please tell me your name: ")
    fileone_file = open("file1.txt", "w")
    a = fileone_file.write(fileone)
    fileone.file.close()
file1()

def file2():
    filetwo = input("Please tell me your favorite color: ")
    filetwo_file = open("file2.txt", "w")
    a = filetwo_file.write(filetwo)
    filetwo.file.close()
file2()

def combinefiles():
    filenames = ['file1.txt', 'file2.txt']
        with open('file3.txt', 'w') as outfile:
            for names in filenames:
                with open(names) as infile:
                    outfile.write(infile.read())
                    outfile.write("\n")
                    output = out_fun()
                    outfile.write(str(output))
        outfile.close()
combinefiles()

Expected text file output on separate lines:预期的文本文件 output 在单独的行上:

John (name)
Blue (color)
12 (random number)

Here is a working version that creates your desired output:这是创建所需 output 的工作版本:

import random

def out_fun():
    return f'Your number is {random.randint(0,5)}'

with open('file3.txt','w') as file:
    file.write(out_fun())

def file1():
    fileone = input('Please tell me your name: ')
    with open('file1.txt', 'w') as fileone_file:
        fileone_file.write(fileone)
file1()

def file2():
    filetwo = input('Please tell me your favorite color: ')
    with open('file2.txt', 'w') as filetwo_file:
        filetwo_file.write(filetwo)
file2()

def combinefiles():
    filenames = ['file1.txt', 'file2.txt']
    with open('file3.txt', 'w') as outfile:
        for name in filenames:
            with open(name) as infile:
                outfile.write(infile.read())
                outfile.write('\n')
        outfile.write(out_fun())
combinefiles()

Note that the final outfile.write call is outside the for loop.请注意,最后的outfile.write调用在for循环之外

A few notes:几点注意事项:

  • You should prefer using with to open files instead of open / close .您应该更喜欢使用with打开文件而不是open / close
  • When using with , you do not normally need to explicitly close files.使用with ,通常不需要显式关闭文件。
  • Remember the difference between printing a value (sending it to the console as a string) and returning a value (using the actual value in the code)记住打印值(将其作为字符串发送到控制台)和返回值(使用代码中的实际值)之间的区别

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

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