Advertisement

08.28.2008 at 08:11PM PDT, ID: 23687820
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

How to modify a file recursively within a directory

Asked by sara_bellum in Python Scripting Language

Tags: , ,

I have a php script with the same file name located in 24 different subdirectories of a parent directory.  Due to changes in the path structure, I need to modify a path on line 7 of each file.  I don't know which language is best suited to this sort of thing but picked python because I'm trying to learn it.  I thought I would start by using csv reader to modify a single file, but even that doesn't work (see below).  

I'm trying to change characters 29 - 36 of line 7 from 'archives' to 'archive_hours' and leave the rest of the line unchanged.  I can print my change (the path variable) to stdout, but only if I quote the line as a string, which defeats the purpose of having a script at all (given that the last part of each path on line 7 is unique).  

Needless to say, I'm unable to change line 7, so the lines that are written to dest currently mirror the original file.  At one point I got the changes to stick but only by defining a new line quoted as a string, and even then the line in the destination file was printed as a list, with each character separated by a comma.  

So the first thing I need to learn is which changes need to happen in what order.  I'm assuming that the command I used to fix the path is incorrect because it creates a string which can't be joined to a list.  But I don't know how else to replace that part of the path -- do I use line.del[29:36] and then line.insert[29:36] with the change to the path?  Whatever I've attempted so far gives me attribute errors.

I've spent enough time on this by now to have manually made the changes several times over, so I figured it was time to ask EE.  My follow-up questions - how to get rid of the windows line returns (^M) and of course how to run this (or another) script so that it will look for the same file name in each subdirectory and make the change - can be deferred as needed (let me know if I'll need to open new questions for that).

TIA
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
#!/usr/bin/python
 
import csv
 
fin = open('sort_archives.php', 'rb')
source = csv.reader(fin)
fout = open('hr_archives.php', 'wb')
dest = csv.writer(fout)
index = 0
for n, line in enumerate(source):
    if n == 6:
          line = "$myDir='/path-to/archives/archive1';"
          path = str(line[29:36]).replace('archive', 'archive_hours')
          print path
          print line[0:]
    dest.writerow(line)
fin.close()
fout.close()
[+][-]08.28.2008 at 08:33PM PDT, ID: 22342158

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.28.2008 at 08:35PM PDT, ID: 22342166

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.28.2008 at 11:20PM PDT, ID: 22342920

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.29.2008 at 03:18AM PDT, ID: 22343862

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.29.2008 at 03:34AM PDT, ID: 22343956

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.29.2008 at 04:37PM PDT, ID: 22349897

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.01.2008 at 02:57AM PDT, ID: 22358775

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Python Scripting Language
Tags: python, 2.4, Linux
Sign Up Now!
Solution Provided By: pepr
Participating Experts: 2
Solution Grade: A
 
 
[+][-]09.02.2008 at 02:37PM PDT, ID: 22371968

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.03.2008 at 01:31PM PDT, ID: 22381633

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080924-EE-VQP-38 / EE_QW_2_20070628