在Python中将文件路径设置为变量

时间:2019-10-10 11:34:55

标签: python python-2.7

我是Python的新手,我正在尝试获取一些代码来将文本文件连接为一个!

我有以下代码:

Checkpoint = open("/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_Checkpoint.txt", "r")
eightbyeight = open("/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_8x8.txt", "r")
AmazonAWS = open("/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_AmazonAWS.txt", "r")
Common_Tech_Terms = open("/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_Common_Tech_Terms.txt", "r")

import sys

filenames = ['Common_Tech_Terms', 'eightbyeight', 'AmazonAWS', 'Checkpoint']
with open('output_file2.txt', 'w+') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            for line in infile:
                outfile.write(line + "\n")

我收到以下错误:

Traceback (most recent call last):
  File "/Users/owenmurray/Desktop/Combining Files", line 60, in <module>
    with open(fname) as infile:
IOError: [Errno 2] No such file or directory: 'Common_Tech_Terms'
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/owenmurray/Desktop/Combining Files"]
[dir: /Users/owenmurray/Desktop]

有人知道要解决此问题吗?

2 个答案:

答案 0 :(得分:1)

您失去了使用上下文管理器的好处,在需要文件内容时调用var req = '{"testId":"12345","ruleId":"678910","rulePassed":true,"testValue":"C:\\ProgramTest\\"}' var stringified = JSON.stringify(req); console.log('stringified json ' + stringified); //json = JSON.parse(JSON.stringify(stringified)) var json = JSON.parse(stringified ); console.log('parsed json ' + json); //testing different ways of pulling out the data, all undefined var testId = json["testId"]; var ruleId = json.ruleId; var testValue = json[testValue]; console.log('testValue ' + testValue); var rulePassed = Boolean(json[rulePassed]); njson = '{"testId": "' + testId + '","ruleId": "' + ruleId + '","testValue": "' + testValue + '","rulePassed": ' + rulePassed + '}'; console.log('final json ' + njson);

open

答案 1 :(得分:0)

如果您这样做:

@media (min-width: 768px) {
    .d-md-columns {
        display: inline-block !important;
        column-count: 2;
        -webkit-column-gap: 0
        -moz-column-gap: 0;
        column-gap: 0;
    }
}

然后,您还需要手动关闭文件:

Checkpoint = open("/Users/owenmurray/Desktop/vocab/Custom_Vocab_Split_by_Brand_Checkpoint.txt", "r")

相反,只需:

Checkpoint.close()
相关问题