-2

Files that are named the same, e.g.

File.txt
File(2).txt
File(3).txt
File(4).txt

And so on. How do I do so that no matter how many files with the same name I throw in in the same directory they will all be edited by my script? Something that makes Python take e.g. File(Some number here).txt and edit them no matter what number is present?

New explanation

param = 'ORIGINAL PARAMS/PARAM.SFO'
with open (param, 'rb') as p, open('PATCHED PARAMS/PARAM.SFO', 'wb') as o:
        o.write(p.read().replace(b'\x85', b'\xA5'))

I want this to open files in the directory 'ORIGINAL PARAMS' with same names as described above. Then it should edit them, then output them to 'PATCHED PARAMS' with the same names but bytes x85 changed to xA5.

Also @BoboDarph, your code didn't work for me.

halfer
  • 19,824
  • 17
  • 99
  • 186
ISAK.M
  • 9
  • 4
  • Actually not, that is not what I am looking for, that's not with numbers. – ISAK.M Mar 04 '19 at 09:46
  • Then what is your question? The example response in the question provides multiple ways of sorting the contents of a list of files based on start and end of filename. Did you try any of the solutions provided in that question? What does your filtering code look like? Your question is unclear to you or your understanding of the provided answer is unclear. I'm working under the assumption of the latter. – BoboDarph Mar 04 '19 at 10:31
  • Okay so I this Is what I have: – ISAK.M Mar 04 '19 at 12:16
  • with open (param, 'rb') as p, open('PATCHED PARAMS/PARAM.SFO', 'wb') as o: o.write(p.read().replace(b'\x85', b'\xA5')) – ISAK.M Mar 04 '19 at 12:16
  • And I want it to output multiple files of that with the same names PARAM.SFO, PARAM.SFO(2), PARAM.SFO(3) etc – ISAK.M Mar 04 '19 at 12:17
  • This is also what param stands for: param = 'ORIGINAL PARAMS/PARAM.SFO' – ISAK.M Mar 04 '19 at 12:19
  • Add your code to the question and your expected result and I will try to provide an answer. – BoboDarph Mar 04 '19 at 12:23

1 Answers1

0

After digging through the comments section, I think I may have a solution for OP's problem. As usual, the question seems to be an X vs Y issue, where OP asks for something but expects something else in return.

So, in accordance to OP's comments, here's a possible implementation.

import os
import inspect


# Assuming you have a project with the following tree structure:
''''<current_dir>
     my_script.py
     ORIGINAL PARAMS
         PARAM.SFO
     ...
     PATCHED PARAMS
         notPARAM.SFO
         PARAM.SFO
         PARAM(1).SFO
         PARAM(2).SFO
         ...
'''''
# The following script will list the contents of PATCHED PARAMS, filter all files that start with PARAM, then for each
# open the source_filename, read it, replace all \x85 binary characters with \xA5 characters in the bytestream
# and write the resulting bytestream to the previously opened PARAM* file.
# Do note that opening a file with wb will either create it if it does not exist, or completely overwrite it
# In order to avoid problems with relative paths, we get the current directory of the script
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
# Knowing the relative structure of our project, we could always assume that the target of our script is in the
# same PATCHED PARAMS folder, so we save that path as another script variable
patched_params_directory = 'PATCHED PARAMS'
patched_params = os.path.join(currentdir, patched_params_directory)
# This would be the initial source file that contains the values (the one you read and get the value to replace)
original_directory = 'ORIGINAL PARAMS'
original_filename = 'PARAM.SFO'
# And this would be it's full path. This is here to avoid issues with relative paths
source_filename = os.path.join(currentdir, os.path.join(original_directory, original_filename))
# This is the prefix of all the files we want to alter
params_file_prefix = 'PARAM'
# For every file in the contents of the PATCHED PARAMS directory that starts with PARAM
# So for example PARAM(1) would be accepted, while 1PARAM would not
# param or PaRaM would also not be accepted. If you want those to be accepted, cast both filenames and prefix to lower
for file in [_ for _ in os.listdir(patched_params) if _.startswith(params_file_prefix)]:
    # Calculate the destination filename based on our filter
    dest_filename = os.path.join(patched_params, file)
    # Open both source and dest files, parse the source, write changes to dest
    with open(dest_filename, 'wb') as f, open(source_filename, 'rb') as p:
        f.write(p.read().replace(b'\x85', b'\xA5'))
BoboDarph
  • 2,751
  • 1
  • 10
  • 15