打开并写入文件

时间:2017-03-31 16:24:45

标签: python python-3.x file text subprocess

我创建了一个代码,要求用户在纯文本文件中输入位置列表,将用户在文本文件中输入的位置保存为列表,而不是要求用户输入每个位置所代表的单词(相同的顺序)作为位置列表)结束重新创建句子。 但是我不确定是什么东西:

1)如何在前一个文档文档关闭时弹出纯文本文档(因此只有当文件:list_of_numbers已关闭时,其他文件list_of_words才会弹出)。

2)如何在纯文本文件上写输出。

这是代码:

import subprocess
subprocess.Popen(["notepad","list_of_numbers.txt"])
with open("list_of_numbers.txt","r") as pos_file:
    positions = pos_file.read().spllit()

subprocess.Popen(["notepad","list_of_words.txt"])
with open("list_of_words.txt","r") as sentence_file:
    words = sentence_file.read().split()

mapping = dict(zip(positions, words))
output = [mapping[position] for position in positions]

print(' '.join(output))

2 个答案:

答案 0 :(得分:0)

我不确定你要做什么,但这应该有所帮助。你不需要使用子进程,事实上它可能使某些事情过于复杂......

输入:

Enter a number. (Leave blank to continue): 1
Enter a number. (Leave blank to continue): 2
Enter a number. (Leave blank to continue): 3
Enter a number. (Leave blank to continue): 


Enter a word. (Leave blank to continue): a
Enter a word. (Leave blank to continue): b
Enter a word. (Leave blank to continue): c
Enter a word. (Leave blank to continue): 

输出(console和to output.txt):

1, a

2, b

3, c

代码:

#!/usr/bin/env python

while True:

    positions = []
    words = []

    while True:
        number = raw_input("Enter a number. (Leave blank to continue): ")
        if not number:
            break

        try:
            positions.append(int(number))
        except TypeError:
            print("Invalid number provided. Try again.")

    print("\n")

    while True:
        word = raw_input("Enter a word. (Leave blank to continue): ")
        if not word:
            break

        words.append(word)

    if len(positions) != len(words):
        print("You did not enter the same number of words and positions..")
        print("Clearing previous inputs..")
    else:
        break

with open("output.txt", 'a') as output_file:
    for x in xrange(0, len(positions)):
        line = "{0}, {1}\n".format(positions[x], words[x])
        output_file.write(line)
        print(line)

答案 1 :(得分:0)

通过查阅Python文档了解如何做到这一点没有问题   - Popen
- Reading and Wirting Files

提示!!! - 如果创建对象,则应将其分配给变量!

# DO
process = subprocess.Popen(["notepad","list_of_numbers.txt"])

# DONT
subprocess.Popen(["notepad","list_of_numbers.txt"])

咨询python文档应该带你到这样的解决方案:

import subprocess

# don't do anything unless child process has been terminated
with subprocess.Popen(["notepad","list_of_numbers.txt"]) as process:
    pass    # you could leave this line blank but using pass clearer

with open("list_of_numbers.txt","r") as pos_file:
    positions = pos_file.read().split()

process = subprocess.Popen(["notepad","list_of_words.txt"])

with open("list_of_words.txt","r") as sentence_file:
    words = sentence_file.read().split()

mapping = dict(zip(positions, words))
output = [mapping[position] for position in positions]

# open a new file in write mode, write data and close the file
with open('new_file.txt', 'w') as f:
    f.write(' '.join(output))

注意:您仍然需要在第17行和第18行修改分配给mappingoutput,因为它们不会产生预期的结果,但是这不是你问题的一部分。