简体   繁体   English

如何制作程序,以便用户可以指定文件名?

[英]How do I make my program so that the user can specify a file name?

I initially wrote a program that would read a file containing text, then would read each line and send it to the output file. 我最初编写了一个程序,该程序将读取包含文本的文件,然后读取每一行并将其发送到输出文件。 Now I am trying to make it so that I can allow the user to specify the file name on the command line and if the user doesn't specify a name, prompt them for one. 现在,我试图这样做,以便允许用户在命令行上指定文件名,如果用户未指定名称,则提示他们输入一个。 Any Idea how to do this? 任何想法如何做到这一点?

#Allow the user to enter the input and output file names
inputFileName = input("Input file name: ")
outputFileName = input("Output file name: ")

#Open the input and output files
infile = open(inputFileName,"r")
outfile = open(outputFileName, "w")

#Define count
count=0

#Reads the input file and produces the output file
line = infile.readline()
while line != "":
count = count+1
print(count, line)
line = infile.readline()

#Close the infile and the outfile    
infile.close()
outfile.close()

Use argparse 使用argparse

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--file', help='file path') #add --file to the args
args = parser.parse_args()

if not(args.file): #if they didn't specify a --file, prompt them
    file = input("Please enter file path?")
else:
    file = args.file #if they did specify file, make file what they specified in the cmd

print(file)

Then you call it with python program.py --file doc.txt 然后使用python program.py --file doc.txt

Edited for your specific case 根据您的具体情况进行编辑

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--input', help="input file")
parser.add_argument('--output', help="output file")
args = parser.parse_args()

if args.input:
    inputFileName = args.input
else:
    inputFileName = input("Input file name: ")

if args.output:
    outputFileName = args.output
else:
    outputFileName = input("Output file name: ")


#Open the input and output files
infile = open(inputFileName,"r")
outfile = open(outputFileName, "w")

#Define count
count=0

#Reads the input file and produces the output file
line = infile.readline()


while line != "":
    count = count+1
    print(count, line)
    line = infile.readline()

#Close the infile and the outfile    
infile.close()
outfile.close()

Suggested Edit 建议编辑

The way you are reading each line is really strange, I would suggest something like this 您阅读每一行的方式真的很奇怪,我建议这样

for line in infile: #loop through every line in infile
    outfile.write(line) #write this line to outfile
    print(line.rstrip("\n")) #print this line, but don't print the \n. that's why you were having multiple line breaks before

If you want to be able to call something like this: 如果您希望能够这样调用:

python program_name outputFileName

then 然后

from sys import argv
try:
    outputFileName = argv[1]
except IndexError:
    outputFileName = input("Output file name: ")

Also, I'm not sure if this was intentional, but your code doesn't actually write anything to outfile. 另外,我不确定这是否是故意的,但是您的代码实际上并未向outfile写入任何内容。

暂无
暂无

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

相关问题 如果用户在我的 if 语句后回答“否”,我如何才能让代码再次询问您的姓名? - How do I make it so that the code will ask you your name again if the user answers no after my if statement? 我如何继续循环我的程序,以便用户可以输入尽可能多的内容? - How do i keep looping the same my program so the user can input as much as they want? 我如何制作用户配置文件,以便我输入姓名、邮件等,然后保存,以便我可以登录 - How do i make a user profile so that i will input the name, mail etc. and then it saves, so i can login PYTHON:如何使程序生成具有随机文件名的文件 - PYTHON : How do I make my program produce a file with a random file name 如果用户说“不”,我该如何做到这一点,以便我的代码可以返回一行 - How do I make it so my code can return to a line if the User says No 我如何让我的程序向用户提出更多问题? - How do i make my program ask further questions to the user? 如何在 plyer 中指定文件类型过滤器,以便用户只允许使用某些扩展名的 select 文件? - How do I specify a file type filter in plyer so that the user is only allowed to select files with certain extenions? 我如何制作这个程序,以便当用户输入用户名时它不接受系统中已经存在的用户名[暂停] - how do i make this program so that when the user inputs a username it does not accept a username that is already in the system [on hold] 事件可以执行命令吗? 如果是这样,我怎样才能让我的人这样做? - Can an event execute a command? If so, how can I make my one do so? 我怎样才能使运行该程序的其他用户不需要手动下载和安装模块? - How can I make it so that manual download and install of modules is not required by another user running the program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM