简体   繁体   English

使用变量python脚本打开文件

[英]Use variables python script to open file

I'm not too familiar with python, can anyone tell me how I can open files using variables in python? 我对python不太熟悉,谁能告诉我如何使用python中的变量打开文件? I want to automate this in a script to run the same task across multiple directories 我想在脚本中自动执行此操作以在多个目录中运行同一任务

Here machine and inputfile are variables.. I tried the following code but keep getting errors 这里的机器和输入文件是变量。.我尝试了以下代码,但不断出错

file = open(machine + '/' + inputfile)

printing works fine.. ie the variables were populated correctly: 打印效果很好..即变量已正确填充:

print 'Input file is "', inputfile -> abc 
print 'Machine "', machine -> xyz

Hence file location should be ./xyz/abc 因此,文件位置应为./xyz/abc

Error: The error I get is file ./machine/inputfile does not exist, ie instead of taking the value of the variable machine and inputfile it is taking them as is. 错误:我得到的错误是文件./machine/inputfile不存在,即,不是采用变量machine和inputfile的值,而是按原样使用它们。

Apologies if this is too trivial a question 道歉,如果这太琐碎的问题

In a general sense, there is nothing wrong with your code. 一般而言,您的代码没有错。 There is probably something wrong with your path and/or file name. 您的路径和/或文件名可能有问题。

This is how I would do it (on windows) 这就是我要怎么做(在Windows上)

import os

dir='C:/Users/xxx' # You can use forward slashes on Windows
file='somefile.txt'

full_path=os.path.join(dir,file) # OS independent way of building paths

with open(full_path,'r') as f: # 'with' will automatically close file for you,
    for line in f: # Do something with the file
        print line

First, I don't recommend naming your variable file because file() is a built-in alias to the open() function in Python. 首先,我不建议命名变量file因为file()是Python中open()函数的内置别名。 You can do this. 可以做到这一点。 But I recommend against it as many Python programmers will cringe when they see it. 但是我建议不要这样做,因为许多Python程序员在看到它时都会畏缩。

Perhaps "in_filename" for the input filename and "inputfile" for the file() object. 也许“ in_filename”代表输入文件名,“ inputfile”代表file()对象。

More to the point once you have the open file you have to call methods on it (explicitly or implicitly) in order to use the contents of the file. 更重要的是,一旦打开了文件,就必须在其上(显式或隐式)调用方法以使用文件的内容。

There are several ways to do this. 有几种方法可以做到这一点。 (As others have pointed out you should use os.path.join() to portably combine a path to a base filename): (正如其他人指出的那样,您应该使用os.path.join()可移植地组合基本文件名的路径):

#!python
import os
path = '/some/path'
in_filename = 'somefile.txt'
inputfile = open(os.path.join(path, in_filename), 'r')
data = inputfile.read()  # fetch all contents in one string
inputfile.seek(0) # reset file object point to beginning of file
data_lines = inputfile.readlines()  # fetch all contents as list of lines
inputfile.seek(0) # rest file object again
while True:
    line = inputfile.readline() # one line at a time (line is a string)
    if not line:
        break   # length of line is zero at end of file
        # Note: empty strings are "False" in Python; so this detects EOF
inputfile.close()  # close inputfile 

# alternatively using "context management" features
with open(os.path.join(path, in_filename), 'r') as infile:
    for line in infile:
        do_something(line)

# infile is implicitly closed on exit from the context management "suite"
# (indented block of code)

In this sample I'm showing how to slurp in the entire file as a single string; 在这个示例中,我展示了如何将整个文件作为单个字符串插入。 how to slurp it in as a list of lines, how to iterate over it line-by-line and how to use the "context manager" ( with statement) to iterate over it line-by-line). 如何将其作为行的列表插入,如何逐行对其进行迭代,以及如何使用“上下文管理器”( with语句)逐行对其进行迭代)。

Notice that iterating over a file is implicitly calling the .readline() (singular) method. 请注意,遍历文件是隐式调用.readline() (单数)方法。 (That's actually orthogonal to the with context management we used to open it. with only guarantees that the file will be automatically closed after the end of our indented block of code ... even if some exception were raised by the open or any other operations we preformed on it. for ... in ...: creates and iterator and uses it; and many objects in Python define iteration semantics. For files that's based on calls to .readline() ) (这实际上是垂直于with上下文管理我们用来打开它。 with只保证文件将我们的代码缩进块结束后自动关闭......即使有一些例外被打开或任何其他操作引发我们对它for ... in ...:预编程, for ... in ...:创建和迭代器并使用它; Python中的许多对象都定义了迭代语义。对于基于.readline()调用的文件)

I'm also showing how to .seek() back to the beginning of the file (which you might not do often but in this case it's a way to show all these alternative ways of reading the file without having to repeatedly .close() and re-open it). 我还展示了如何.seek()返回文件的开头(您可能不经常这样做,但是在这种情况下,这是一种显示所有其他可选方式来读取文件的方法,而无需重复执行.close()然后重新打开)。

Note that I'm explicitly use the access option in my open() call to declare that my file handler will be read-only. 请注意,我在open()调用中显式使用访问选项来声明我的文件处理程序将为只读。 If I wanted to read from and write to the file I'd use 'w+' and so on). 如果我想读取和写入文件,请使用'w+' ,依此类推。

I'm not showing the option to the .read() method which allows you to specify the amount of data you wish to read for each call. 我没有显示.read()方法的选项,该选项使您可以指定希望为每个调用读取的数据量。 For normal text files this is generally not very useful because then you'd have to handle any partial lines yourself which is rather messy code. 对于普通的文本文件,这通常不是很有用,因为那样一来,您就必须自己处理任何分行,这会导致代码混乱。 If you were to attempt to read some very large file using my examples then you might cause your system to page/swap and suffer some drastic performance and responsiveness issues with your system. 如果您尝试使用我的示例读取一些非常大的文件,则可能导致系统分页/交换,并且系统遭受严重的性能和响应性问题。 (On a typical, modern, laptop you'll be fine so long as the file is less than a gigabyte or two in size). (在典型的现代笔记本电脑上,只要文件大小小于1 GB或2 GB,就可以了)。

Using .read(xxx) (with specific numbers of characters to be read for any call) can be useful if you have a file containing fixed length records (text or binary). 如果您的文件包含固定长度的记录(文本或二进制),则使用.read(xxx) (对于任何调用都应读取特定数量的字符)会很有用。 However, it's rare these days for anyone to manipulation fixed length file structures using Python or other scripting languages. 但是,如今很少有人使用Python或其他脚本语言来操纵定长文件结构。 If you were dealing with non-text file (such as manipulating the headers of MP3 files, or images, etc, then you might use such methods (and you'd likely want to also use the struct module to "pack" data from its textual representation into the specific machine-native representation required by your binary file formats). 如果您要处理非文本文件(例如处理MP3文件的标题或图像等),则可以使用此类方法(并且您可能还希望使用struct模块从其“包装”数据文本表示形式转换为二进制文件格式所需的特定机器本地表示形式)。

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

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