读取日志文件并在那里打开文件

时间:2016-03-13 15:07:03

标签: python python-3.x logging

我在读取我正在另一种方法中创建的日志文件时遇到问题。 日志文件中包含解压缩文件的文件路径(解压缩在另一个函数中)。日志文件看起来有点像这样

/home/usr/Downloads/outdir/Code/XXX.something
/home/usr/Downloads/outdir/Code/YYY.something
/home/usr/Downloads/outdir/Code/AAA.something
@staticmethod
def iterate_log(path):
    results = str()
    for dirpath, dirnames, files in os.walk(path):
        for name in files:
            results += '%s\n' % os.path.join(dirpath, name)
    with open(os.path.join(EXTRACT_PATH_, LOGNAME_), 'w') as logfile:
        logfile.write(results)
        return os.path.join(EXTRACT_PATH_, LOGNAME_)

@staticmethod
def read_received_files(from_file):
    with open(from_file, 'r') as data:
        data = data.readlines()
        for lines in data:
        # to reduce confusion I would use
        # for line in data:
            read_files = open(lines)
            print(read_files)
            return read_files

现在我想逐行读取日志文件(第二种方法中的参数from_files是第一种方法的返回值)并打开这些文件并返回它们(在别处使用它们)。 到目前为止readlines()read()都给了我错误

readlines() = [Errno2] No sucht file or directory: '/../../../logfile.log\n

read() = IsADirectoryError: [Errno21]

整个追溯:

    Traceback (most recent call last):
  File "files_received_test.py", line 13, in <module>
    main()
  File "files_received_test.py", line 9, in main
    j = Filesystem.execute_code(FILENAME_)
  File "/home/usr/PycharmProjects/Projektgruppe/code/src/filesystem_hasher.py", line 16, in execute_code
    Filesystem.read_received_files(create_log)
  File "/home/usr/PycharmProjects/Projektgruppe/code/src/filesystem_hasher.py", line 54, in read_received_files
    read_files = open(lines)
FileNotFoundError: [Errno 2] No such file or directory: '/home/usr/Downloads/outdir/unpacked_files.log\n'

1 个答案:

答案 0 :(得分:0)

您只需要删除换行符,然后更改

read_files = open(lines.strip())

'/home/usr/Downloads/outdir/unpacked_files.log\n'

作为观察 - 对于读取错误消息中的每个字符至关重要 - 消息告诉您没有名为

的文件
    import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


    VivzHelper vivzHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vivzHelper= new VivzHelper(this);

       // vivzHelper.insertEntry();


      //  vivzHelper.deleteAll();
        SQLiteDatabase data = vivzHelper.getWritableDatabase();




       // Cursor todoCursor = data.rawQuery("Select * from books", null);
       // ListView lvItems = (ListView) findViewById(R.id.list);
      //  TodoCursorAdapter todoAdapter = new TodoCursorAdapter(this, todoCursor);
      //  lvItems.setAdapter(todoAdapter);


    }
    public void openinfo()
    {
        Intent intent = new Intent(this, ShowData.class);
        startActivity(intent);
    }




}

所以有必要尝试理解为什么\ n出现 - 这与你对文件名的期望不符,所以你应该想知道为什么文件有那个名字