Copy files with timestamp into folders and subfolders, which are built in year/month/day in Python
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
add a comment |
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
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
add a comment |
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
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
python
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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()
add a comment |
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')
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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()
add a comment |
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()
add a comment |
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()
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()
answered Nov 14 '18 at 9:34
Andrey SemakinAndrey Semakin
153110
153110
add a comment |
add a comment |
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')
add a comment |
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')
add a comment |
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')
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')
answered Nov 14 '18 at 9:15
Sagi GolanSagi Golan
413
413
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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