Python Forum
n00b help with referencing files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
n00b help with referencing files
#1
Hey guys,

I have a script that basically modifies a file. In a nutshell, fileA contains some text, and the script reads fileA and uses the text to modify fileB. fileA is in a different directory than fileB, and I have multiple iterations of this. So I've hardcoded the paths in the script to fileA and fileB, but basically I need the script to iterate over multiple paths that contain fileA and fileB. Hopefully this makes sense.

example;

/fruit/oranges/fileA /veggies/tomato/fileB
/fruit/banana/fileA /veggies/lettuce/fileB
/fruit/apple/fileA /veggies/carrot/fileB


Would os.walk come into play here? I'm sure this is a basic function but I'm new to scripting and I'm not sure the technical jargon to search for what I'm trying to do. Any guidance would be appreciated.


my code here
Reply
#2
I don't think os.walk would work here. It could find all the fileA's and all the fileB's, but how would it know that the oranges fileA matches with the tomato fileB?

The matching pairs would need to be hard coded or put into a data file that can be read. If your folder structure is consistent you could loop over the pairs:

pairs = [('oranges', 'tomato'), ('banana', 'lettuce'), ('apple', 'carrot')]
def file_mod(pathA, pathB):
    fileA = open(pathA)
    fileB = open(pathB, 'w')
    # modify file...
for nameA, nameB in pairs:
    file_mod('/fruit/{}/fileA'.format(nameA), '/veggies/{}/fileB'.format(nameB))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Quote:I have a script that basically modifies a file. In a nutshell, fileA contains some text, and the script reads fileA and uses the text to modify fileB. fileA is in a different directory than fileB, and I have multiple iterations of this. So I've hardcoded the paths in the script to fileA and fileB, but basically I need the script to iterate over multiple paths that contain fileA and fileB. Hopefully this makes sense.

Maybe you post some content of fileA and of fileB and your script.
My line of thinking: paths in fileA and fileB should be relative. Then you should have a absolute base directory stores inside your script or as env variable or in a config file. Then joining them together os.path.join(base_path, path_from_text_file). This prevents you from manipulating the path with string interpolation and it is os independent. But when the files are already done and have for example 16k lines, you must handle it like ichabod801 has shown. My suggestion is always, don't use absolute paths in files, which you want to process later. If you want to move the files to a different place and all paths are absolute, you'll have to handle this in your script also or doing the trick with a symlink.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
This is my current code.  So the refFile will be in /some/dir/path(A-Z)/file.  The filethatneedsmodified will be in /some/other/dir/path(A-Z)/filethatneedsmodified.  I'm not sure how to iterate or loop over these dirs, and creating individual scripts for each file\dir obviously isn't the best way to do it.  Can I use os.path.join or something to define the two paths since both file names will remain the same across the different directories?

import fileinput
import sys

def insert_to_line(f_name):
  for line in fileinput.input(f_name, inplace=True):
      if any(item in line for item in theList) and not line.lstrip().startswith('#'):
          sys.stdout.write('# {}'.format(line))
      else:
          sys.stdout.write(line)

if __name__ == '__main__':
  with open("/some/dir/path/file", 'r') as refFile:
   theList = refFile.read().splitlines()
   f_name = "/some/other/dir/path/filethatneedsmodified"
   insert_to_line(f_name)
Reply
#5
Given a refFile, how do you know what path to use for the modified file? It is in any way predictable? Or is it like your original post, where the two are completely unrelated?
Reply
#6
Sorry, I'm most likely not explaining it well. The name of the refFile will always be the same, but will be in different paths. There is a corresponding file that needs to be modified that will also be named the same, also in different paths. So /this/path/to/A/refFile will correspond to /this/other/different/path/to/A/filetomodify. Next will be /this/path/to/B/refFile and will correspond to /this/other/different/path/to/B/filetomodify, and so on. The reference file is always named the same, and that will correspond to a file that needs to be modified, which will also be named the same throughout the different paths.

/this/path/to/A/refFile --> /this/other/different/path/to/A/filetomodify
/this/path/to/B/refFile --> /this/other/different/path/to/B/filetomodify
/this/path/to/C/refFile --> /this/other/different/path/to/C/filetomodify
/this/path/to/D/refFile --> /this/other/different/path/to/D/filetomodify
Reply
#7
I don't think you need os.walk(), since you're not recursing into those directories (it would work, it's just more complicated than you need).  glob.glob() could do this just fine.

import glob, pathlib

destination = "/this/other/different/path/to/{0}/filetomodify"

for ref in glob.glob("/this/path/to/*/refFile"):
   path = pathlib.Path(ref)
   folder = path.parent.name
   with open(ref) as refFile:
       with open(destination.format(folder), "w") as file_to_modify:
           for line in refFile:
               # line formatting here
               print(line, file=file_to_modify)
Reply
#8
Thanks for the help.

Maybe it's something I'm doing wrong, but it seems it's not able to use destination = "/this/other/different/path/to/{0}/filetomodify". If I add that path to the glob I'm able to print the files. It seems it doesnt like the {0}. If I glob with a /*/ it works.
Reply
#9
{0} is str.format() syntax.  Which is why this line contains .format(): with open(destination.format(folder), "w") as file_to_modify:.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  name 'lblstatus' is not defined when referencing a label KatManDEW 4 1,546 Apr-21-2022, 12:33 PM
Last Post: KatManDEW
  Dictionary Referencing nickdavis2017 1 1,615 Nov-20-2021, 06:24 PM
Last Post: deanhystad
  Referencing string names in df to outside variables illmattic 1 1,370 Nov-16-2021, 12:47 PM
Last Post: jefsummers
  Referencing a fixed cell Mark17 2 2,074 Dec-17-2020, 07:14 PM
Last Post: Mark17
Sad need help in referencing a list n00bdev 2 1,857 Nov-01-2020, 12:06 PM
Last Post: buran
  Issue referencing new instance from other class nanok66 3 2,245 Jul-31-2020, 02:07 AM
Last Post: nanok66
  referencing another method in a class Skaperen 6 2,679 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  Theory behind referencing a dictionary rather than copying it to a list sShadowSerpent 2 2,096 Mar-24-2020, 07:18 PM
Last Post: sShadowSerpent
  n00b question millpond 6 3,383 Jul-13-2019, 06:41 AM
Last Post: Gribouillis
  "not defined" error in function referencing a class Exsul 2 3,690 Mar-27-2019, 11:59 PM
Last Post: Exsul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020