简体   繁体   中英

Python script looking for nonexistent file

I don't know Python at all but I have to get this script running soon. I've searched for similar problems online but I can't find the exact scenario.

I ran a script nameTime.py which is supposed to output the usernames and dates from the access_log into a list that contains the number and names of each user for each day in the log. The script didn't finish and outputted the following error.

Traceback (most recent call last):
    File "./nameTime.py", line 19, in <module>
        f=open(file,'r')
IOError: [Errno 2] No such file or directory: 'access_log-20150215'

I copy it from /etc/httpd/logs into /opt/logs and it's the only access_log in that directory. BTW, the exact same script with modified os.chdir(...) and if file.startswith(...) commands, works in the subdirectory /opt/logs/ion (but does not have the nonexistent file in it either).

First lines of script:

import os
import re
os.chdir("opt/logs")

user = []
dates = []
userlist = set()
userPerDay = []
name = re.compile(r'\s[A-Z][A-Z\s*]+\.[A-Z]+[A-Z\s*]*\.[A-Z]+[A-Z\s*]*(?=\.[0-9]+)')
longname = re.compile(r'\s[A-Z][A-Z\s*]+\.[A-Z]+[A-Z\s*]*\.[A-Z]+[A-Z\s*]*\.[A-Z]+[A-Z\s*](?=\.[0-9]+)')
date = re.compile('[0-3][0-9]\/[A-Za-z]+\/[0-9]+(?=:)')
for root, dirs, files in os.walk("./"):
    for file in files:
        if file.startswith("access_"):
            f=open(file,'r')

It seems that the name "file" still contains the value of that old filename. I don't see any name "file" assigned that value to it at all in the script. Since I only want this script to process the only access log in /opt/logs, access_log, apparently I have to clear out that old value (or however it's done); how do I do it?

There's no guarantee that the file you are looking for is in the current directory.

You need to join the file path root when opening the file:

import os.path

../..
f = open(os.path.join(root,file),'r')
../..

The os.chdir('opt/logs') is also probably wrong (it assumes that the script is run from a specific directory) - you're better off using an absolute path (eg /opt/logs or whatever is appropriate in your case).

Check the root variable. You might be looking for the file at ./access_log-20150215 that is actually in a subdirectory such as ./subdir/access_log-20150215 .

If you want to explore all subdirectories use

f=open(os.path.join(root,file),'r')

but if you only want the top directory you can use

os.listdir('.')

OK, so starting at the top of the suggestions from @James and @isedev, I first tried os.listdir('.') and... it worked. I ran nameTime.py in the /opt/logs folder to only process access_log (and not look for access_log-20150215 or any other) and got my output file, dateAccess.txt, just the way I needed it. Thanks for your other suggestions too; I'm sure I'll have to tweak this file at some point to run on our other systems to provide more statistics, so my next thing on the list is to study up on Python so your other suggestions will make sense to me. Thanks very much!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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