It is subtle whether it is as efficient as it is, but I tried it in between work.
It will copy some files locally from the file server. Also create a directory.
I am assigned a very new daily task of printing out the task list for the day and distributing it to the person in charge. You need to be prepared to create a directory locally and copy the files from the file server to it. I thought that if I wrote a little code, it would be ready with just one click, so I tried it.
Create a directory to store the files for that day locally according to the rule yyyymmdd. After that, copy the task list file from the file server.
First, get the time when the script is executed, and then get the date as a character string based on it.
os.mkdir(path)Create a directory with. This is the end of the first stage.
Next, get the file list of the directory containing the task list of the file server with ```os.listdir (path) `` `.
Since the file name to be copied has a common character string [hoge] at the beginning, you can search for the corresponding file with `` `str.startswith (prefix)` ``.
Since the file list is a list, it searches for files one by one in a loop, and as soon as it is found, it is copied to the directory created from the file server with `` `shutil.copy (src, dst)` ``.
This completes the preparation for daily work. All you have to do is print it out.
#encode:utf-8
import os import shutil from datetime import datetime
if name == "main":
#Get the current time
today = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
y = datetime.now().strftime('%Y')
m = datetime.now().strftime('%m')
d = datetime.now().strftime('%d')
#Folder name(yyyymmdd)
dirname = y + m + d
#Create folder
os.mkdir(dirname)
#Directory with files you want to copy.
copydir = 'B:/foo/bar/baz/'
#Get a list of files placed in a directory
files = os.listdir(copydir)
for n in range(len(files)):
#Copy files with common strings to the yyyymmdd folder
if files[n].startswith('【hoge】'):
print(files[n])
shutil.copy(copydir + files[n] , './' + dirname + '/' + files[n])
### [2016.10.01 postscript]
[Correction]
Since it is clearly wrong, I corrected it to ```encode: utf-8``` → ```encoding: utf-8```. Thank you for pointing out in the comments.
**【sad news】**
It was said that the daily work was completed as the phase progressed.
It's the code I wrote, but it doesn't seem to come into play for a while ('ω')
However, since the function is simple, it may be applicable to other applications.
Recommended Posts