Python Forum
n00b help with referencing files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
n00b help with referencing files
#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


Messages In This Thread
n00b help with referencing files - by theturd - Jul-11-2017, 03:58 PM
RE: n00b help with referencing files - by DeaD_EyE - Jul-11-2017, 05:14 PM
RE: n00b help with referencing files - by theturd - Jul-19-2017, 02:16 PM
RE: n00b help with referencing files - by nilamo - Jul-19-2017, 03:19 PM
RE: n00b help with referencing files - by theturd - Jul-19-2017, 03:54 PM
RE: n00b help with referencing files - by nilamo - Jul-19-2017, 04:09 PM
RE: n00b help with referencing files - by theturd - Jul-21-2017, 03:17 PM
RE: n00b help with referencing files - by nilamo - Jul-21-2017, 04:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  name 'lblstatus' is not defined when referencing a label KatManDEW 4 1,633 Apr-21-2022, 12:33 PM
Last Post: KatManDEW
  Dictionary Referencing nickdavis2017 1 1,647 Nov-20-2021, 06:24 PM
Last Post: deanhystad
  Referencing string names in df to outside variables illmattic 1 1,409 Nov-16-2021, 12:47 PM
Last Post: jefsummers
  Referencing a fixed cell Mark17 2 2,130 Dec-17-2020, 07:14 PM
Last Post: Mark17
Sad need help in referencing a list n00bdev 2 1,881 Nov-01-2020, 12:06 PM
Last Post: buran
  Issue referencing new instance from other class nanok66 3 2,284 Jul-31-2020, 02:07 AM
Last Post: nanok66
  referencing another method in a class Skaperen 6 2,748 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  Theory behind referencing a dictionary rather than copying it to a list sShadowSerpent 2 2,137 Mar-24-2020, 07:18 PM
Last Post: sShadowSerpent
  n00b question millpond 6 3,440 Jul-13-2019, 06:41 AM
Last Post: Gribouillis
  "not defined" error in function referencing a class Exsul 2 3,730 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