首先是目录。
file1.py
|
| --__init__.py
file2.py
some_package
|
| --__init__.py
config.py
settings.ini
other_package
|
| --__init__.py
access.py
现在,在config.py
中,有一个函数readfromsetting()
,它读取settings.ini
并返回其中的内容。在access.py
中,我导入了config.py
(不,这不是您的想法,access.py
成功导入config.py
),并尝试调用readfromsetting()
函数,但是Python会抛出错误。
No such file or directory, "settings.ini"
所以,我的问题是,如何使用settings.ini
从access.py
阅读config.py
的内容?
我的config.py
:
def readfromsetting():
with open('settings.py', 'r') as file:
return file.read()
我的access.py
from some_package import config
def get_setting():
return config.readfromsetting()
答案 0 :(得分:2)
执行package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount2 {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString()," \t\n\r\f,.:;?![]'");
while (itr.hasMoreTokens()) {
word.set(itr.nextToken().toLowerCase());
context.write(word, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
if (sum > 4) context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount2.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
for (int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}
FileOutputFormat.setOutputPath(job,
new Path(otherArgs[otherArgs.length - 1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
时,python查看import <some script>
的内容以导入python脚本。但是当您使用sys.path
或其他方法读取文件并且提供相对路径时,Python会尝试将路径解析为相对于当前工作目录的路径,它不会查看脚本所在的目录(除非那是工作目录。)
您不应该依赖相对路径,而应尽可能尝试给出绝对路径。
在您的情况下,如果您确定目录结构不会更改(open()
将始终位于settings.ini
存在或config.py
存在的目录中),那么您可以使用settings.py
变量来访问文件的路径,然后使用它来创建__file__
的绝对路径。示例 -
settings.ini
答案 1 :(得分:0)
你的问题在这里:
def readfromsetting():
with open('settings.ini', 'r') as file:
return file.read()
&#39;的Settings.ini&#39;是一条相对的道路。如果您要查找的文件位于同一目录中,则此方法非常有用。由于您从access.py调用此方法,而access.py不在此目录中,因此无法找到settings.ini文件。
我建议您在open方法中使用settings.ini文件的绝对路径。如果您确定只在access.py中调用此方法,则还可以编辑其他答案中提到的__file__
。