- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章python检测文件夹变化,并拷贝有更新的文件到对应目录的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
检测文件夹,拷贝有更新的文件到对应目录 2016.5.19 。
亲测可用,若有借鉴请修改下文件路径; 。
学习python小一个月后写的这个功能,属于初学,若有大神路过,求代码优化~ 。
newcopy.py:
检测文件夹中最后修改时间变化的文件,并拷贝复制到相应路径下,拷贝目录会自动检测后输出;测试文件夹路径记得修改; 。
pyinotify.py:
借用window接口,检测脚本所在目录下文件夹变化(更新、删除、添加等),输出日志到桌面上; 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# newcopy.py文件
# -*- coding:UTF-8 -*-
import
os
import
os.path
import
sys
import
time
import
datetime
import
stat
import
difflib
import
linecache, shutil
# 文件全路径和对应最后修改时间写入到out.txt文档中;
def
add_log(path):
with
open
(
'out.txt'
,
'w'
) as f:
f.close()
for
root , dirs, files
in
os.walk(path):
for
name
in
files:
temp_path
=
os.path.join(root,name)
file_name
=
temp_path.replace(
'C:/Users/Enter/Desktop/'
, '')
file_time
=
os.stat(temp_path).st_mtime
with
open
(
'out.txt'
,
'a'
) as f:
f.write(
','
.join( [
'%s'
%
file_name ,
'%s\n'
%
file_time] ) )
f.close()
# 注意时间格式转换
#file_time = time.localtime(os.stat(root).st_mtime)
#file_time=date.strftime('%Y-%m-%d %H:%M:%S')
def
if_exist():
# 判断文件out.txt是否存在,不存在则创建
filename
=
'out.txt'
if
os.path.exists(filename):
message
=
'OK, the "%s" file exists.'
else
:
message
=
"Sorry, I cannot find the '%s' file..and I create it."
a
=
open
(
'out.txt'
,
'w'
)
a.close()
print
message
%
filename
# 判断update文件夹是否存在,不存在则创建
files_name
=
'update'
if
os.path.exists(files_name):
message
=
'OK, the "%s" file exists.'
else
:
message
=
"Sorry, I cannot find the '%s' file.and I create it. "
os.mkdir(
'update'
)
print
message
%
files_name
# path 待比较的文件夹路径
# 返回生成的txt(包含更新或者添加的文件路径)的路径
def
log_compare(path):
# 先确保out.txt存在
if_exist()
# 获取out.txt文件内容(文件全路径key和最后修改时间value),生成dict
txt
=
open
(
'out.txt'
,
'r'
).readlines()
myDic
=
{}
for
row
in
txt:
(key, value)
=
row.split(
','
)
myDic[key]
=
value
print
myDic
# 创建以时间命名的文件和文件夹
setup_filename
=
str
(datetime.datetime.now().strftime(
'%Y%m%d%H%M%S'
))
# 获取当前时间
setup_file_path
=
'%s%s.txt'
%
(
'C:/Users/Enter/Desktop/update/'
,setup_filename)
# 生成以当前时间命名的.txt文件,准备写入更新日志
setup_file_dir
=
'%s%s'
%
(
'C:/Users/Enter/Desktop/update/'
,setup_filename)
# 生成以当前时间命名的.txt文件夹
#判断key,比较value值是否变化
#原始需要有一个out.txt文件,才能比较value确定是否有更新
#运行程序时,重新遍历一遍文件全路径和最后修改时间
for
root , dirs, files
in
os.walk(path):
for
name
in
files:
temp_path
=
os.path.join(root,name)
file_name
=
temp_path.replace(
'C:/Users/Enter/Desktop/'
, '')
time
=
os.stat(temp_path).st_mtime
# 获取最后修改时间
file_time
=
'%s\n'
%
time
# 加%s\n是为了与out.txt里值完全对应
if
myDic.has_key(file_name)
=
=
True
:
if
cmp
(myDic[file_name], file_time):
# myDic[file_name]旧最后修改时间,file_time新最后修改时间
print
(file_name,file_time)
# 输出有变化的文件名及其对应的最后修改时间
# 输出以文件时间命名的更新日志,生成路径是update下
with
open
(setup_file_path,
'a'
) as f:
# 有更新的文件,写入更新日志
f.write(
'%s\n'
%
file_name )
f.close()
else
:
print
"add"
,file_name
with
open
(setup_file_path,
'a'
) as f:
# 新增的文件,写入更新日志
f.write(
'%s\n'
%
file_name )
f.close()
# 返回 当前时间,以时间命名的文件夹路径,更新文件路径
return
(setup_filename, setup_file_dir, setup_file_path)
# 将src目录中的内容拷贝到dest目录
# 如果dest或者其子目录不存在,先创建
# txt_path为更新日志路径,有更新的文件才拷贝
def
copy_directory(src, dest, txt_path):
if
not
os.path.exists(txt_path):
print
"no file update"
return
# 读更新日志,获取更新文件的全路径
txt
=
open
(txt_path,
'r'
).readlines()
myDic
=
{}
myDic2
=
{}
for
row
in
txt:
myDic[row]
=
"1"
tempArray
=
os.path.split(row)
key
=
tempArray[
0
]
myDic2[key]
=
"1"
print
"myDic2:"
, myDic2
print
"dict:"
, myDic
# 遍历原始文件夹,得到所有文件的全路径
for
root, dirs, files
in
os.walk(src):
for
name
in
files:
#print "dirs:",dirs
fpath
=
os.path.join(root, name)
newroot
=
root
newroot
=
newroot.replace(src, dest)
# 根据文件绝对路径,创建将要拷贝的路径(相对路径),没有则创建
#print newroot
rel_dir
=
root.replace(
'C:/Users/Enter/Desktop/'
, '')
if
not
os.path.exists(newroot)
and
myDic2.has_key(rel_dir):
print
"rel_dir:"
, rel_dir
print
newroot
os.makedirs(newroot)
os.chmod(newroot, stat.S_IWRITE)
temp
=
fpath
temp
=
temp.replace(src, dest)
rel_path
=
fpath.replace(
'C:/Users/Enter/Desktop/'
, '')
# 将绝对路径改为相对路径,便于遍历对比,挑出要拷贝的文件
rel_path
+
=
'\n'
if
myDic.has_key(rel_path)
=
=
True
:
print
"real_path:"
, rel_path
# os.mkdir(rel_path)
shutil.copy(fpath, temp)
print
"copyfile:"
, fpath
def
main():
path_dir
=
'C:/Users/Enter/Desktop/acd'
path_file
=
'C:/Users/Enter/Desktop/out.txt'
params
=
log_compare(path_dir)
add_log(path_dir)
copy_directory(path_dir, params[
1
], params[
2
])
if
__name__
=
=
'__main__'
:
main()
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#pyinotify.py文件
# -*- coding:UTF-8 -*-
import
os
import
win32file
import
win32con
# #检测当前目录下所有文件删除、更新、修改等变化。更新日志输出到桌面。2016.5.23 copy
ACTIONS
=
{
1
:
"Created"
,
2
:
"Deleted"
,
3
:
"Updated"
,
4
:
"Renamed from something"
,
5
:
"Renamed to something"
}
# Thanks to Claudio Grondi for the correct set of numbers
FILE_LIST_DIRECTORY
=
0x0001
path_to_watch
=
"."
hDir
=
win32file.CreateFile (
path_to_watch,
FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
None
,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None
)
while
1
:
#
# ReadDirectoryChangesW takes a previously-created
# handle to a directory, a buffer size for results,
# a flag to indicate whether to watch subtrees and
# a filter of what changes to notify.
#
# NB Tim Juchcinski reports that he needed to up
# the buffer size to be sure of picking up all
# events when a large number of files were
# deleted at once.
#
results
=
win32file.ReadDirectoryChangesW (
hDir,
1024
,
True
,
win32con.FILE_NOTIFY_CHANGE_FILE_NAME |
win32con.FILE_NOTIFY_CHANGE_DIR_NAME |
win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES |
win32con.FILE_NOTIFY_CHANGE_SIZE |
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE |
win32con.FILE_NOTIFY_CHANGE_SECURITY,
None
,
None
)
#print "results:", results
for
action,
file
in
results:
full_filename
=
os.path.join (path_to_watch,
file
)
print
full_filename, ACTIONS.get (action,
"Unknown"
)
with
open
(
'C:/Users/Enter/Desktop/fileupdate.txt'
,
'a'
) as f:
#str = ','.join( ['%s' % full_filename , '%s\n' % ACTIONS.get (action, "Unknown")] )
#print str
f.write(
','
.join( [
'%s'
%
full_filename ,
'%s\n'
%
ACTIONS.get (action,
"Unknown"
)] ) )
f.close()
|
以上这篇python检测文件夹变化,并拷贝有更新的文件到对应目录的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我.
原文链接:https://blog.csdn.net/Tina224243/article/details/51853732 。
最后此篇关于python检测文件夹变化,并拷贝有更新的文件到对应目录的方法的文章就讲到这里了,如果你想了解更多关于python检测文件夹变化,并拷贝有更新的文件到对应目录的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
嘿伙计们。 实现背景变化(基本上是幻灯片放映)和过渡效果的常见方法有哪些。我想每隔一段时间改变complte文档背景。 我是一名 ASP.net 开发人员,并且希望大部分内容都可以在 ASP 中实现。
也许,指针已经在修改过程中指向 auto_ptr 的常规指针指向 unique_ptr 和 shared_ptr 我只是想知道已经开发出来的新型指针是否完全覆盖了旧版本(或者您可能认为存在内存泄漏问题
我使用 Android Studio 构建 Android 应用。 我的问题是:当 fragment 改变时,应用程序崩溃。 控制台输出[控制台] 01-06 18:35:21.952 27756-
****澄清**我做了这个 [Fiddle] ( http://jsfiddle.net/sggPv/10/ ) 来帮助澄清情况。 该脚本起初适用于两个表格,但随后当您点击 slider 并将新表格加
我有图标,单击它会将新的 div(列)添加到 div 容器。问题是,当新的 div(列)出现时,按钮不会向右移动。是否可以以某种方式仅在 div 内添加 position:fixed? 这是我的几个屏
我是 Java 新手,继承了现有的 Android 应用程序。原始开发人员选择使用常量接口(interface)。 我的问题是我需要更改其中一些常量来编译生产应用程序与开发应用程序。如果我手动修改一些
在 Apple developer Document 中,我在 UIColor 中发现了一些新东西。 If your app was linked on or after iOS 10 and whe
我没有经常使用 ShareKit,但我只想拥有三个共享选项:Facebook、Twitter 和电子邮件。 ShareKit 提供了更多选项,包括更多按钮。但是,我不想要“更多”选项,只想要三个。 在
我正在构建一个 JS 库,其中一个用例要求我在 DOM 更改时触发一个事件,特别是如果它是一个单页应用程序,例如:github search bar 经过一番研究,我遇到了MutationObserv
我已经设法编写了一个代码来检测任何工作表中特定单元格的值变化,但我一直在努力构建检测和跟踪范围(值)变化的东西。 例如,如果用户决定复制和粘贴某个范围的数据(假设超过 1 个单元格),它不会被宏捕获。
使用 ffmpeg ,我们可以对音频电平进行多少控制?例如,我想在程序的时间轴上映射一个“M”形: t0 - t1 : fade in from 0 to 1 t1 - t2 : play at fu
使用 jQuery 1.7.1,我尝试为下拉列表上的更改事件创建一个事件处理程序。下拉列表会动态添加到 DOM 中。似乎在大多数浏览器上都能很好地工作,但是哦,奇怪的 IE8 想要变得困难。有解决方法
我想制作一个具有可选边框大小的自定义控件。请参阅下面的代码。边框绘制在非客户区,其宽度可以是 0、1 或 2 像素。我已经在 WM_NCPAINT 中成功完成了边框绘制。问题是,在更改控制边框大小的属
我知道这个问题之前已经被问过,而且我实际上已经找到了一些我已经实现的解决方案。不幸的是,我没能得到我想要的。 我以前没有做过AngularJS,我想做的是: 检测网址何时更改 根据网址更改的内容进行一
我有一个 auto-carousel 指令,它循环访问链接元素的子元素。 但是,子级尚未加载到 DOM 中,因为它们的 ng-if 表达式尚未解析。 如何确保父指令知道其 DOM 树已发生更改?
我有一个流程可以通过内容提供商从数据库中获取数据。 fun getDataFlow(): Flow { return flow { emit(Result.Loading)
我有一些有效的代码,但有时它只是“跳转”到其他文本而不考虑间隔。 该代码基本上按时间间隔更改标题的文本。 var text = ["text1", "text2", "text3","text4","
我正在尝试将 onCLick 监听器添加到我的 PreferenceScreen 上的开关,但它不起作用。我尝试了 Java 教程中的代码并将其转换为 Kotlin,但由于某种原因它无法正常工作。 这
我们目前正在尝试升级我们的程序使用的 ffmpeg 版本。跳跃很大,因为我们目前使用的是 ffmpeg 0.8,最新版本是 1.2。 在这些测试中,我使用的是(让我说)我发现的令人惊叹的软件包 her
我有一个流程可以通过内容提供商从数据库中获取数据。 fun getDataFlow(): Flow { return flow { emit(Result.Loading)
我是一名优秀的程序员,十分优秀!