Copy files with timestamp into folders and subfolders, which are built in year/month/day in Python










-2















I have a question. I have files in different folders and subfolders. I have created a new file system in a new environment by year, month and day. I want to save the files there, I would like to read the files according to their timestamps and store them automatically in the designated folders. How do you do that best?










share|improve this question

















  • 1





    What is the format of the files? Where are your attempts to solve this problem?

    – Tomothy32
    Nov 14 '18 at 9:07











  • See this link - stackoverflow.com/help/how-to-ask

    – Nick
    Nov 14 '18 at 9:11















-2















I have a question. I have files in different folders and subfolders. I have created a new file system in a new environment by year, month and day. I want to save the files there, I would like to read the files according to their timestamps and store them automatically in the designated folders. How do you do that best?










share|improve this question

















  • 1





    What is the format of the files? Where are your attempts to solve this problem?

    – Tomothy32
    Nov 14 '18 at 9:07











  • See this link - stackoverflow.com/help/how-to-ask

    – Nick
    Nov 14 '18 at 9:11













-2












-2








-2








I have a question. I have files in different folders and subfolders. I have created a new file system in a new environment by year, month and day. I want to save the files there, I would like to read the files according to their timestamps and store them automatically in the designated folders. How do you do that best?










share|improve this question














I have a question. I have files in different folders and subfolders. I have created a new file system in a new environment by year, month and day. I want to save the files there, I would like to read the files according to their timestamps and store them automatically in the designated folders. How do you do that best?







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:06









mgm841mgm841

25




25







  • 1





    What is the format of the files? Where are your attempts to solve this problem?

    – Tomothy32
    Nov 14 '18 at 9:07











  • See this link - stackoverflow.com/help/how-to-ask

    – Nick
    Nov 14 '18 at 9:11












  • 1





    What is the format of the files? Where are your attempts to solve this problem?

    – Tomothy32
    Nov 14 '18 at 9:07











  • See this link - stackoverflow.com/help/how-to-ask

    – Nick
    Nov 14 '18 at 9:11







1




1





What is the format of the files? Where are your attempts to solve this problem?

– Tomothy32
Nov 14 '18 at 9:07





What is the format of the files? Where are your attempts to solve this problem?

– Tomothy32
Nov 14 '18 at 9:07













See this link - stackoverflow.com/help/how-to-ask

– Nick
Nov 14 '18 at 9:11





See this link - stackoverflow.com/help/how-to-ask

– Nick
Nov 14 '18 at 9:11












2 Answers
2






active

oldest

votes


















1














Here is my solution:



import os
from datetime import datetime

SRC_PATH = './src'
DST_PATH = './dst'

for dirpath, _, filenames in os.walk(SRC_PATH):
for filename in filenames:
src_filepath = os.path.join(dirpath, filename)
last_edit = datetime.fromtimestamp(os.path.getmtime(src_filepath))

dst_dirpath = os.path.join(DST_PATH, str(last_edit.year), str(last_edit.month), str(last_edit.day))
dst_filepath = os.path.join(dst_dirpath, filename)

os.makedirs(dst_dirpath, exist_ok=True)
os.rename(src_filepath, dst_filepath)

print(src_filepath, '->', dst_filepath)


It will walk recursively your directory structure and move all files to another directory named by YEAR/MONTH/DAY of file modification.



Docs:




  • os.walk()

  • os.makedirs()





share|improve this answer






























    0














    You can simply use datetime to format timestamp to string and then define you folder name



    from datetime import datetime
    ts = 1514101485
    folder_name = datetime.utcfromtimestamp(ts).strftime('%Y_%m_%d')





    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%2f53296441%2fcopy-files-with-timestamp-into-folders-and-subfolders-which-are-built-in-year-m%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














      Here is my solution:



      import os
      from datetime import datetime

      SRC_PATH = './src'
      DST_PATH = './dst'

      for dirpath, _, filenames in os.walk(SRC_PATH):
      for filename in filenames:
      src_filepath = os.path.join(dirpath, filename)
      last_edit = datetime.fromtimestamp(os.path.getmtime(src_filepath))

      dst_dirpath = os.path.join(DST_PATH, str(last_edit.year), str(last_edit.month), str(last_edit.day))
      dst_filepath = os.path.join(dst_dirpath, filename)

      os.makedirs(dst_dirpath, exist_ok=True)
      os.rename(src_filepath, dst_filepath)

      print(src_filepath, '->', dst_filepath)


      It will walk recursively your directory structure and move all files to another directory named by YEAR/MONTH/DAY of file modification.



      Docs:




      • os.walk()

      • os.makedirs()





      share|improve this answer



























        1














        Here is my solution:



        import os
        from datetime import datetime

        SRC_PATH = './src'
        DST_PATH = './dst'

        for dirpath, _, filenames in os.walk(SRC_PATH):
        for filename in filenames:
        src_filepath = os.path.join(dirpath, filename)
        last_edit = datetime.fromtimestamp(os.path.getmtime(src_filepath))

        dst_dirpath = os.path.join(DST_PATH, str(last_edit.year), str(last_edit.month), str(last_edit.day))
        dst_filepath = os.path.join(dst_dirpath, filename)

        os.makedirs(dst_dirpath, exist_ok=True)
        os.rename(src_filepath, dst_filepath)

        print(src_filepath, '->', dst_filepath)


        It will walk recursively your directory structure and move all files to another directory named by YEAR/MONTH/DAY of file modification.



        Docs:




        • os.walk()

        • os.makedirs()





        share|improve this answer

























          1












          1








          1







          Here is my solution:



          import os
          from datetime import datetime

          SRC_PATH = './src'
          DST_PATH = './dst'

          for dirpath, _, filenames in os.walk(SRC_PATH):
          for filename in filenames:
          src_filepath = os.path.join(dirpath, filename)
          last_edit = datetime.fromtimestamp(os.path.getmtime(src_filepath))

          dst_dirpath = os.path.join(DST_PATH, str(last_edit.year), str(last_edit.month), str(last_edit.day))
          dst_filepath = os.path.join(dst_dirpath, filename)

          os.makedirs(dst_dirpath, exist_ok=True)
          os.rename(src_filepath, dst_filepath)

          print(src_filepath, '->', dst_filepath)


          It will walk recursively your directory structure and move all files to another directory named by YEAR/MONTH/DAY of file modification.



          Docs:




          • os.walk()

          • os.makedirs()





          share|improve this answer













          Here is my solution:



          import os
          from datetime import datetime

          SRC_PATH = './src'
          DST_PATH = './dst'

          for dirpath, _, filenames in os.walk(SRC_PATH):
          for filename in filenames:
          src_filepath = os.path.join(dirpath, filename)
          last_edit = datetime.fromtimestamp(os.path.getmtime(src_filepath))

          dst_dirpath = os.path.join(DST_PATH, str(last_edit.year), str(last_edit.month), str(last_edit.day))
          dst_filepath = os.path.join(dst_dirpath, filename)

          os.makedirs(dst_dirpath, exist_ok=True)
          os.rename(src_filepath, dst_filepath)

          print(src_filepath, '->', dst_filepath)


          It will walk recursively your directory structure and move all files to another directory named by YEAR/MONTH/DAY of file modification.



          Docs:




          • os.walk()

          • os.makedirs()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 9:34









          Andrey SemakinAndrey Semakin

          153110




          153110























              0














              You can simply use datetime to format timestamp to string and then define you folder name



              from datetime import datetime
              ts = 1514101485
              folder_name = datetime.utcfromtimestamp(ts).strftime('%Y_%m_%d')





              share|improve this answer



























                0














                You can simply use datetime to format timestamp to string and then define you folder name



                from datetime import datetime
                ts = 1514101485
                folder_name = datetime.utcfromtimestamp(ts).strftime('%Y_%m_%d')





                share|improve this answer

























                  0












                  0








                  0







                  You can simply use datetime to format timestamp to string and then define you folder name



                  from datetime import datetime
                  ts = 1514101485
                  folder_name = datetime.utcfromtimestamp(ts).strftime('%Y_%m_%d')





                  share|improve this answer













                  You can simply use datetime to format timestamp to string and then define you folder name



                  from datetime import datetime
                  ts = 1514101485
                  folder_name = datetime.utcfromtimestamp(ts).strftime('%Y_%m_%d')






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 9:15









                  Sagi GolanSagi Golan

                  413




                  413



























                      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%2f53296441%2fcopy-files-with-timestamp-into-folders-and-subfolders-which-are-built-in-year-m%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

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo