Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6619497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:58:37+00:00 2026-05-25T20:58:37+00:00

Trying to implement a little script to move the older log files out of

  • 0

Trying to implement a little script to move the older log files out of apache (actually using a simple bash script to do this in ‘real life’ – this is just an exercise to practice using Python). I’m getting the filename as a string as the variable f, but I want this to actually be a file when i pass it to self.processFile(root, f, age, inString).

I tried opening the actual file a few different ways, but I’m missing the target, and end up getting an error, a path that doesn’t always seem to be correct, or just a string. I’ll blame it on the late night, but I’m blanking on the best way to open f as a file right before passing to self.processFile (where it will be gzipped). Usually its something very simple that i’m missing, so I have to assume that’s the case here. I’d appreciate any constructive advice/direction.

 """recursive walk through /usr/local/apache2.2/logs"""
    for root, dirs, files in os.walk(basedir):
            for f in files:
                m=self.fileFormatRegex.match(f)
                if m:
                    if (('access_log.' in f) or
                        ('error.' in f) or
                        ('access.' in f) or
                        ('error_log.' in f) or
                        ('mod_jk.log.' in f)):
                        #This is where i'd like to open the file using the filename f
                        self.processFile(root, f, age, inString)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:58:37+00:00Added an answer on May 25, 2026 at 8:58 pm

    Use os.path.abspath:

    self.processFile(root, open(os.path.abspath(f)), age, inString)
    

    Like so:

    import os
    for root, dirs, files in os.walk(basedir):
        for f in files:
            m=self.fileFormatRegex.match(f)
            if m:
                if (set('access_log.', 'error.', 'access.', 'error_log.','mod_jk.log.').intersection(set(f))):
                    self.processFile(root, open(os.path.abspath(f)), age, inString)
    

    Or os.path.join:

    import os
    for root, dirs, files in os.walk(basedir):
        for f in files:
            m=self.fileFormatRegex.match(f)
            if m:
                if (set('access_log.', 'error.', 'access.', 'error_log.','mod_jk.log.').intersection(set(f))):
                 self.processFile(root, open(os.path.join(r"/", root, f)), age, inString)
                 # Sometimes the leading / isnt necessary, like this:
                 # self.processFile(root, open(os.path.join(root, f)), age, inString)
    

    More about os.path


    Yet another way using file() instead of open() (does almost the same thing as open):

    self.processFile(root, file(os.path.join(root, f), "r"), age, inString)
    self.processFile(root, file(os.path.abspath(f), "r+"), age, inString)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a very simple little dropdown-style mini-menu that will appear
I'm trying to implement is_base template and I have a little problem. Why this
I'm trying to implement a little server service in order to upload files via
Trying to implement Piwik using REST API over http but need a little help.
trying to implement a dialog-box style behaviour using a separate div section with all
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I'm trying to implement something like this: <div> <table> <thead> <tr> <td>Port name</td> <td>Current
I am trying to implement a little 'intranet' on my home network, but I
I'm trying to implement scripting capability to my application. I'm using the code below.
I'm trying to implement a little function in my app. I am currently playing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.