简体   繁体   English

如何执行python程序并自动以不同的文件名保存输出

[英]How to execute python program and save output in different file names automatically

I have a Python program which processes images and save the resulting output to a file.我有一个 Python 程序,它处理图像并将结果输出保存到文件中。 I am receiving the input and output file names via console using input().我正在使用 input() 通过控制台接收输入和输出文件名。 But I want to save the output automatically with the file name as something like "input_file_name_out".但我想用文件名自动保存输出,如“input_file_name_out”。 Note the string "_out" attached to the input file name请注意附加到输入文件名的字符串“_out”

This will help me get user input only for Input file names thereby saving little time for the user since he doesn't need to think everytime what should be the output file name这将帮助我仅获取输入文件名的用户输入,从而为用户节省很少的时间,因为他不需要每次都考虑输出文件名应该是什么

Sample code snippet示例代码片段

if __name__ == "__main__":

    in_file = input()
    out_file = input()

    processed = process_image(in_file,out_file)

Expected code snippet预期的代码片段

if __name__ == "__main__":

    in_file = input()
    # out_file = input() ------Not getting the file name from user thereby supplying only one argument to the below function

        processed = process_image(in_file)

# And within the function
def process_image(img):
    .
    .
    .
    out_file = img+"_out.jpg" ##### What should come here to achieve my requirement ########
    cv2.imwrite(out_file,processed_image)

you could make use of the Path class from the pathlib module:您可以使用pathlib模块中的Path类:

from pathlib import Path

in_file = Path(input())
insert = '_out'

out_file = in_file.parent / (in_file.stem + insert + in_file.suffix)

Example:例子:

in_file = Path('D:/folder/test.jpg')
insert = '_out'
out_file = in_file.parent / (in_file.stem + insert + in_file.suffix)

print(out_file)
D:\folder\test_out.jpg

All you have to do is,你所要做的就是,

  • Take the input_img.jpg that you get from user input从用户输入中获取input_img.jpg
  • Split the input_img.jpg with delimiter .用 delimiter 分割input_img.jpg . and that will give you this list: ['input_img', 'jpg']这会给你这个列表: ['input_img', 'jpg']
  • Pick up the first element and add _out.jpg suffix拾取第一个元素并添加_out.jpg后缀
def process_image(img):

    # you should split with delimiter ('.'), take the first 
    # element of the list and add "_out.jpg" suffix

    split_name = img.split('.')
    out_file = split_name[0] + "_out" + "." + split_name[1] 
    cv2.imwrite(out_file, processed_image)


if __name__ == "__main__":

    in_file = input()
    processed = process_image(in_file)

Well that's simple!嗯,这很简单! Just take the "in_file" string you asked for, and concat "_out" to the end and pass it as the out_file.只需获取您要求的“in_file”字符串,并将“_out”连接到最后并将其作为out_file传递。

If you only process ".jpg"如果您只处理“.jpg”

cv2.imwrite(in_file[:-4]+"_out.jpg",processed_image)

if its variable:如果它的变量:

cv2.imwrite(in_file[:-4]+"_out"+in_file[-4:],processed_image)

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

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