0

I have a working code that takes a list of file paths and reads them iteratively. For example:

class moveDcmNrrd4mJson_v3(object):
  def __init__(self, jsonList, dataPath, dframe):
     self.jsonList = jsonList #providing a list
     self.dataPath = dataPath
     self.dframe = dframe

  def MoveFiles(self):
     for jsonidx, jsonpath in enumerate(self.jsonList):
        print("\n")
        print(">>> Reading {} of {} json file: {}".format(jsonidx+1, len(self.jsonList), os.path.basename(jsonpath)))

That works when more than one file paths are in the list. for example when: ['/media/banikr2/CAP_Exam_Data0/CAP Session Backups/wilist_all_MV_20180914-1053.json', '/media/banikr2/CAP_Exam_Data0/CAP Session Backups/wilist_all_MV_20180914-1418.json', '/media/banikr2/CAP_Exam_Data0/CAP Session Backups/wilist_all_MV_20180914-1452.json', '/media/banikr2/CAP_Exam_Data0/CAP Session Backups/wilist_all_MV_20180914-1517.json', '/media/banikr2/CAP_Exam_Data0/CAP Session Backups/wilist_all_MV_20180914-1705.json']

I want to make it work when only single filepath. For example when I call jsonList[-1] with the same enumerate loop. Now the loop just iterate over the path ASCII characters.

Calling the code as:

A = moveDcmNrrd4mJson_v3(jsonList[2], capDir, df) # only one filepath
A.MoveFiles()

I have seen some suggestions here in this post however not exactly clear to me.

banikr
  • 63
  • 1
  • 9

1 Answers1

-1

It seems like you may be passing the single path in as a string instead of a list. Make sure it is being passed as a list. If this doesn't answer it for you, could you share a little more about the jsonList?

SunnyNonsense
  • 410
  • 3
  • 22
  • Please do not post comments and questions as answers. – DYZ Jul 14 '21 at 01:55
  • `['/media/banikr2/CAP_Exam_Data0/CAP Session Backups/wilist_all_MV_20180914-1053.json', '/media/banikr2/CAP_Exam_Data0/CAP Session Backups/wilist_all_MV_20180914-1418.json', '/media/banikr2/CAP_Exam_Data0/CAP Session Backups/wilist_all_MV_20180914-1452.json', '/media/banikr2/CAP_Exam_Data0/CAP Session Backups/wilist_all_MV_20180914-1517.json', '/media/banikr2/CAP_Exam_Data0/CAP Session Backups/wilist_all_MV_20180914-1705.json']` This is the `jsonList` with 5 filepaths which works in my code. But I want to make it work for a single path like `jsonList[2]` which is a `str` I know. – banikr Jul 14 '21 at 01:57
  • @DYZ, if he is in fact passing the single path as a string, is it not the answer (or really one of many answers)? Would it have been more acceptable if I left out the last sentence hedging my answer? banikr, is there a reason to not simply convert your single string to a list? – SunnyNonsense Jul 14 '21 at 02:01
  • I don't know why the question was closed without clarification. This is passing a string of filepath but the other question is just numbers which is two different thing – banikr Jul 14 '21 at 02:03
  • @banikr It _is_ the same thing. A list of one number is constructed exactly like a list of one string. – DYZ Jul 14 '21 at 02:13
  • banikr, some people here are too smart to realize that everything is not so obvious without years of experience. If there is a reason the single file path must always come in as a string, I would suggest putting a simple if statement ahead of your print statement to convert it to a single item list <> (next line) <>. This should allow your code to function the way it is currently written. I guarantee you there's a better way of doing it, but I'm not the smartest guy here, just trying to get you something that works. – SunnyNonsense Jul 14 '21 at 02:30