Applying function to a list of file-paths and writing csv output to the respective paths












0















How do I apply a function to a list of file paths I have built, and write an output csv in the same path?




read file in a subfolder -> perform a function -> write file in the
subfolder -> go to next subfolder




#opened xml by filename
with open(r'XML_opsReport 100001.xml', encoding = "utf8") as fd:
Odict_parsedFromFilePath = xmltodict.parse(fd.read())

#func called in func below
def activity_to_df_one_day (list_activity_this_day):
ib_list = [pd.DataFrame(list_activity_this_day[i], columns=list_activity_this_day[i].keys()).drop("@uom") for i in range(len(list_activity_this_day))]
return pd.concat(ib_list)

#Processes parsed xml and writes csv
def activity_to_df_all_days (Odict_parsedFromFilePath, subdir): #writes csv from parsed xml after some processing
nodes_reports = Odict_parsedFromFilePath['opsReports']['opsReport']
list_activity =
for i in range(len(nodes_reports)):
try:
df = activity_to_df_one_day(nodes_reports[i]['activity'])
list_activity.append(df)

except KeyError:
continue
opsReport = pd.concat(list_activity)
opsReport['dTimStart'] = pd.to_datetime(opsReport['dTimStart'], infer_datetime_format =True)
opsReport.sort_values('dTimStart', axis=0, ascending=True, inplace=True, kind='quicksort', na_position='last')
opsReport.to_csv("subdiropsReport.csv") #write to the subdir




def scanfolder(): #fetches list of file-paths with desired starting name.

list_files =

for path, dirs, files in os.walk(r'C:..xml_objects'): #directory containing several subfolders
for f in files:
if f.startswith('XML_opsReport'):
list_files.append(os.path.join(path, f))
return list_files

filepaths = scanfolder() #list of file-paths


Every function works well, the xml processing is good, so I am not sharing the xml structure. There are 100+ paths in filepaths , each a different subdirectory. I want to be able to apply above flow in future as well, where I can get filepaths and perform desired actions. It's important to write the csv file to it's sub directory.










share|improve this question

























  • It's not totally clear to me what you're requesting; Is this helpful? stackoverflow.com/questions/35315873/…

    – Evan
    Nov 20 '18 at 19:46











  • I was able to solve

    – pyeR_biz
    Nov 20 '18 at 20:45






  • 1





    You can post your own solution and accept it.

    – Evan
    Nov 20 '18 at 20:45
















0















How do I apply a function to a list of file paths I have built, and write an output csv in the same path?




read file in a subfolder -> perform a function -> write file in the
subfolder -> go to next subfolder




#opened xml by filename
with open(r'XML_opsReport 100001.xml', encoding = "utf8") as fd:
Odict_parsedFromFilePath = xmltodict.parse(fd.read())

#func called in func below
def activity_to_df_one_day (list_activity_this_day):
ib_list = [pd.DataFrame(list_activity_this_day[i], columns=list_activity_this_day[i].keys()).drop("@uom") for i in range(len(list_activity_this_day))]
return pd.concat(ib_list)

#Processes parsed xml and writes csv
def activity_to_df_all_days (Odict_parsedFromFilePath, subdir): #writes csv from parsed xml after some processing
nodes_reports = Odict_parsedFromFilePath['opsReports']['opsReport']
list_activity =
for i in range(len(nodes_reports)):
try:
df = activity_to_df_one_day(nodes_reports[i]['activity'])
list_activity.append(df)

except KeyError:
continue
opsReport = pd.concat(list_activity)
opsReport['dTimStart'] = pd.to_datetime(opsReport['dTimStart'], infer_datetime_format =True)
opsReport.sort_values('dTimStart', axis=0, ascending=True, inplace=True, kind='quicksort', na_position='last')
opsReport.to_csv("subdiropsReport.csv") #write to the subdir




def scanfolder(): #fetches list of file-paths with desired starting name.

list_files =

for path, dirs, files in os.walk(r'C:..xml_objects'): #directory containing several subfolders
for f in files:
if f.startswith('XML_opsReport'):
list_files.append(os.path.join(path, f))
return list_files

filepaths = scanfolder() #list of file-paths


Every function works well, the xml processing is good, so I am not sharing the xml structure. There are 100+ paths in filepaths , each a different subdirectory. I want to be able to apply above flow in future as well, where I can get filepaths and perform desired actions. It's important to write the csv file to it's sub directory.










share|improve this question

























  • It's not totally clear to me what you're requesting; Is this helpful? stackoverflow.com/questions/35315873/…

    – Evan
    Nov 20 '18 at 19:46











  • I was able to solve

    – pyeR_biz
    Nov 20 '18 at 20:45






  • 1





    You can post your own solution and accept it.

    – Evan
    Nov 20 '18 at 20:45














0












0








0








How do I apply a function to a list of file paths I have built, and write an output csv in the same path?




read file in a subfolder -> perform a function -> write file in the
subfolder -> go to next subfolder




#opened xml by filename
with open(r'XML_opsReport 100001.xml', encoding = "utf8") as fd:
Odict_parsedFromFilePath = xmltodict.parse(fd.read())

#func called in func below
def activity_to_df_one_day (list_activity_this_day):
ib_list = [pd.DataFrame(list_activity_this_day[i], columns=list_activity_this_day[i].keys()).drop("@uom") for i in range(len(list_activity_this_day))]
return pd.concat(ib_list)

#Processes parsed xml and writes csv
def activity_to_df_all_days (Odict_parsedFromFilePath, subdir): #writes csv from parsed xml after some processing
nodes_reports = Odict_parsedFromFilePath['opsReports']['opsReport']
list_activity =
for i in range(len(nodes_reports)):
try:
df = activity_to_df_one_day(nodes_reports[i]['activity'])
list_activity.append(df)

except KeyError:
continue
opsReport = pd.concat(list_activity)
opsReport['dTimStart'] = pd.to_datetime(opsReport['dTimStart'], infer_datetime_format =True)
opsReport.sort_values('dTimStart', axis=0, ascending=True, inplace=True, kind='quicksort', na_position='last')
opsReport.to_csv("subdiropsReport.csv") #write to the subdir




def scanfolder(): #fetches list of file-paths with desired starting name.

list_files =

for path, dirs, files in os.walk(r'C:..xml_objects'): #directory containing several subfolders
for f in files:
if f.startswith('XML_opsReport'):
list_files.append(os.path.join(path, f))
return list_files

filepaths = scanfolder() #list of file-paths


Every function works well, the xml processing is good, so I am not sharing the xml structure. There are 100+ paths in filepaths , each a different subdirectory. I want to be able to apply above flow in future as well, where I can get filepaths and perform desired actions. It's important to write the csv file to it's sub directory.










share|improve this question
















How do I apply a function to a list of file paths I have built, and write an output csv in the same path?




read file in a subfolder -> perform a function -> write file in the
subfolder -> go to next subfolder




#opened xml by filename
with open(r'XML_opsReport 100001.xml', encoding = "utf8") as fd:
Odict_parsedFromFilePath = xmltodict.parse(fd.read())

#func called in func below
def activity_to_df_one_day (list_activity_this_day):
ib_list = [pd.DataFrame(list_activity_this_day[i], columns=list_activity_this_day[i].keys()).drop("@uom") for i in range(len(list_activity_this_day))]
return pd.concat(ib_list)

#Processes parsed xml and writes csv
def activity_to_df_all_days (Odict_parsedFromFilePath, subdir): #writes csv from parsed xml after some processing
nodes_reports = Odict_parsedFromFilePath['opsReports']['opsReport']
list_activity =
for i in range(len(nodes_reports)):
try:
df = activity_to_df_one_day(nodes_reports[i]['activity'])
list_activity.append(df)

except KeyError:
continue
opsReport = pd.concat(list_activity)
opsReport['dTimStart'] = pd.to_datetime(opsReport['dTimStart'], infer_datetime_format =True)
opsReport.sort_values('dTimStart', axis=0, ascending=True, inplace=True, kind='quicksort', na_position='last')
opsReport.to_csv("subdiropsReport.csv") #write to the subdir




def scanfolder(): #fetches list of file-paths with desired starting name.

list_files =

for path, dirs, files in os.walk(r'C:..xml_objects'): #directory containing several subfolders
for f in files:
if f.startswith('XML_opsReport'):
list_files.append(os.path.join(path, f))
return list_files

filepaths = scanfolder() #list of file-paths


Every function works well, the xml processing is good, so I am not sharing the xml structure. There are 100+ paths in filepaths , each a different subdirectory. I want to be able to apply above flow in future as well, where I can get filepaths and perform desired actions. It's important to write the csv file to it's sub directory.







python-3.x pandas os.path






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 17:16







pyeR_biz

















asked Nov 20 '18 at 16:36









pyeR_bizpyeR_biz

328214




328214













  • It's not totally clear to me what you're requesting; Is this helpful? stackoverflow.com/questions/35315873/…

    – Evan
    Nov 20 '18 at 19:46











  • I was able to solve

    – pyeR_biz
    Nov 20 '18 at 20:45






  • 1





    You can post your own solution and accept it.

    – Evan
    Nov 20 '18 at 20:45



















  • It's not totally clear to me what you're requesting; Is this helpful? stackoverflow.com/questions/35315873/…

    – Evan
    Nov 20 '18 at 19:46











  • I was able to solve

    – pyeR_biz
    Nov 20 '18 at 20:45






  • 1





    You can post your own solution and accept it.

    – Evan
    Nov 20 '18 at 20:45

















It's not totally clear to me what you're requesting; Is this helpful? stackoverflow.com/questions/35315873/…

– Evan
Nov 20 '18 at 19:46





It's not totally clear to me what you're requesting; Is this helpful? stackoverflow.com/questions/35315873/…

– Evan
Nov 20 '18 at 19:46













I was able to solve

– pyeR_biz
Nov 20 '18 at 20:45





I was able to solve

– pyeR_biz
Nov 20 '18 at 20:45




1




1





You can post your own solution and accept it.

– Evan
Nov 20 '18 at 20:45





You can post your own solution and accept it.

– Evan
Nov 20 '18 at 20:45












2 Answers
2






active

oldest

votes


















1














To get the directory that a file is in, you can use:



import os

for root, dirs, files, in os.walk(some_dir):
for f in files:
print(root)
output_file = os.path.join(root, "output_file.csv")
print(output_file)


Is that what you're looking for?



Output:



somedir
somediroutput_file.csv


See also Python 3 - travel directory tree with limited recursion depth and Find current directory and file's directory.






share|improve this answer































    0














    Was able to solve with os.path.join.



        exceptions_path_list =
    for i in filepaths:
    try:
    with open(i, encoding = "utf8") as fd:
    doc = xmltodict.parse(fd.read())
    activity_to_df_all_days (doc, i)
    except ValueError:
    exceptions_path_list.append(os.path.dirname(i))
    continue

    def activity_to_df_all_days (Odict_parsedFromFilePath, filepath):
    ...
    ...
    ...
    opsReport.to_csv(os.path.join(os.path.dirname(filepath), "opsReport.csv"))





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397533%2fapplying-function-to-a-list-of-file-paths-and-writing-csv-output-to-the-respecti%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      To get the directory that a file is in, you can use:



      import os

      for root, dirs, files, in os.walk(some_dir):
      for f in files:
      print(root)
      output_file = os.path.join(root, "output_file.csv")
      print(output_file)


      Is that what you're looking for?



      Output:



      somedir
      somediroutput_file.csv


      See also Python 3 - travel directory tree with limited recursion depth and Find current directory and file's directory.






      share|improve this answer




























        1














        To get the directory that a file is in, you can use:



        import os

        for root, dirs, files, in os.walk(some_dir):
        for f in files:
        print(root)
        output_file = os.path.join(root, "output_file.csv")
        print(output_file)


        Is that what you're looking for?



        Output:



        somedir
        somediroutput_file.csv


        See also Python 3 - travel directory tree with limited recursion depth and Find current directory and file's directory.






        share|improve this answer


























          1












          1








          1







          To get the directory that a file is in, you can use:



          import os

          for root, dirs, files, in os.walk(some_dir):
          for f in files:
          print(root)
          output_file = os.path.join(root, "output_file.csv")
          print(output_file)


          Is that what you're looking for?



          Output:



          somedir
          somediroutput_file.csv


          See also Python 3 - travel directory tree with limited recursion depth and Find current directory and file's directory.






          share|improve this answer













          To get the directory that a file is in, you can use:



          import os

          for root, dirs, files, in os.walk(some_dir):
          for f in files:
          print(root)
          output_file = os.path.join(root, "output_file.csv")
          print(output_file)


          Is that what you're looking for?



          Output:



          somedir
          somediroutput_file.csv


          See also Python 3 - travel directory tree with limited recursion depth and Find current directory and file's directory.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 19:48









          EvanEvan

          1,141516




          1,141516

























              0














              Was able to solve with os.path.join.



                  exceptions_path_list =
              for i in filepaths:
              try:
              with open(i, encoding = "utf8") as fd:
              doc = xmltodict.parse(fd.read())
              activity_to_df_all_days (doc, i)
              except ValueError:
              exceptions_path_list.append(os.path.dirname(i))
              continue

              def activity_to_df_all_days (Odict_parsedFromFilePath, filepath):
              ...
              ...
              ...
              opsReport.to_csv(os.path.join(os.path.dirname(filepath), "opsReport.csv"))





              share|improve this answer




























                0














                Was able to solve with os.path.join.



                    exceptions_path_list =
                for i in filepaths:
                try:
                with open(i, encoding = "utf8") as fd:
                doc = xmltodict.parse(fd.read())
                activity_to_df_all_days (doc, i)
                except ValueError:
                exceptions_path_list.append(os.path.dirname(i))
                continue

                def activity_to_df_all_days (Odict_parsedFromFilePath, filepath):
                ...
                ...
                ...
                opsReport.to_csv(os.path.join(os.path.dirname(filepath), "opsReport.csv"))





                share|improve this answer


























                  0












                  0








                  0







                  Was able to solve with os.path.join.



                      exceptions_path_list =
                  for i in filepaths:
                  try:
                  with open(i, encoding = "utf8") as fd:
                  doc = xmltodict.parse(fd.read())
                  activity_to_df_all_days (doc, i)
                  except ValueError:
                  exceptions_path_list.append(os.path.dirname(i))
                  continue

                  def activity_to_df_all_days (Odict_parsedFromFilePath, filepath):
                  ...
                  ...
                  ...
                  opsReport.to_csv(os.path.join(os.path.dirname(filepath), "opsReport.csv"))





                  share|improve this answer













                  Was able to solve with os.path.join.



                      exceptions_path_list =
                  for i in filepaths:
                  try:
                  with open(i, encoding = "utf8") as fd:
                  doc = xmltodict.parse(fd.read())
                  activity_to_df_all_days (doc, i)
                  except ValueError:
                  exceptions_path_list.append(os.path.dirname(i))
                  continue

                  def activity_to_df_all_days (Odict_parsedFromFilePath, filepath):
                  ...
                  ...
                  ...
                  opsReport.to_csv(os.path.join(os.path.dirname(filepath), "opsReport.csv"))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 21:08









                  pyeR_bizpyeR_biz

                  328214




                  328214






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397533%2fapplying-function-to-a-list-of-file-paths-and-writing-csv-output-to-the-respecti%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                      Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                      A Topological Invariant for $pi_3(U(n))$