如何使python脚本用于移动文件更快?
嗨,我是Python的新手,我制作了这个简单的程序来整理下载文件夹。当我在下载文件夹中有不错的文件时运行此程序时,大约需要10到15秒。
我听说Python相对于C的
最佳方法使该代码在没有并行线程的情况下更有效吗?
另外,通过并行运行线程使其更快地使其更快?
我的下一个计划是使用python为桌面仪表板制作GUI,在那里我可以将此代码作为可运行的图标。我怎么能实现这一目标...任何想法都将不胜感激。感谢您的帮助。
import shutil
import os, time
start = time.time()
print("Sorting Downlaods Folder...")
download_folder = "C:/Users/muham/Downloads"
images = "C:/Users/muham/Pictures"
documents = "C:/Users/muham/Documents"
docs = ["docx", ".txt", ".doc"]
imgs = [".png", ".jpg", "jpeg"]
prgrms = [".exe", ".php", ".c", "java", ".msi"]
for file in os.listdir(download_folder):
if file[-4:] in docs:
shutil.move(download_folder + '/' + file, documents + '/Docs/' + file)
elif file[-4:] == "xlsx" or file[-4:] == ".csv":
shutil.move(download_folder + '/' + file, documents + '/Excel/' + file)
elif file[-4:] == ".pdf" or file[-4:] == ".ppt" or file[-4:] == ".pptx":
shutil.move(download_folder + '/' + file, documents + '/PDFs/' + file)
elif file[-4:] in imgs:
shutil.move(download_folder + '/' + file, images + '/' + file)
elif file[-4:] in prgrms or file[-2:] == ".c":
shutil.move(download_folder + '/' + file, "D:/programs/" + file)
else:
shutil.move(download_folder + '/' + file, "D:/zips/" + file)
print("Download folder sorted")
end = time.time()
print("Time taken:" , end - start)
Hi I am new to python and I made this simple program to sort out my downloads folder. When I run this program when i have decent number of files in download folder, it takes roughly around 10 to 15 seconds.
I heard python is slower with respect to C.
Best way to make this code more efficient without parallel threads?
Also, Best way to make it faster with parallel running threads?
My next plan is to use python to make a GUI for a desktop dashboard where I can have this code as runnable icon. How can I achieve that... any ideas would be greatly appreciated. Thanks for the help.
import shutil
import os, time
start = time.time()
print("Sorting Downlaods Folder...")
download_folder = "C:/Users/muham/Downloads"
images = "C:/Users/muham/Pictures"
documents = "C:/Users/muham/Documents"
docs = ["docx", ".txt", ".doc"]
imgs = [".png", ".jpg", "jpeg"]
prgrms = [".exe", ".php", ".c", "java", ".msi"]
for file in os.listdir(download_folder):
if file[-4:] in docs:
shutil.move(download_folder + '/' + file, documents + '/Docs/' + file)
elif file[-4:] == "xlsx" or file[-4:] == ".csv":
shutil.move(download_folder + '/' + file, documents + '/Excel/' + file)
elif file[-4:] == ".pdf" or file[-4:] == ".ppt" or file[-4:] == ".pptx":
shutil.move(download_folder + '/' + file, documents + '/PDFs/' + file)
elif file[-4:] in imgs:
shutil.move(download_folder + '/' + file, images + '/' + file)
elif file[-4:] in prgrms or file[-2:] == ".c":
shutil.move(download_folder + '/' + file, "D:/programs/" + file)
else:
shutil.move(download_folder + '/' + file, "D:/zips/" + file)
print("Download folder sorted")
end = time.time()
print("Time taken:" , end - start)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先要做的就是介绍此代码,以找出花费时间的时间。按以下顺序优化操作:
#2是两种类型的操作之间的平衡行为,但通常频繁和短暂的选择将是更好的选择。
如果大部分时间都花在
shutil.move()
中,则最多可以将运行时减少一半。实际上,您将无法减少它。在这种情况下,多线程是合适的,因为操作是I/O-BOND。缺少个人资料信息,以下是我能做的最好的信息。
提高速度的最简单方法是更改
doc
,imgs
和prgms
set> set
s。通常,对于具有两个以上元素的集合,set
比list
要快。另一个机会在于使用
str.rsplit()
而不是切片以获取文件扩展名。切片是一个新的字符串,此代码每个循环多次可做。在条件块之前,在循环的开始时进行一次。这将需要从docs
,imgs
和prgms
中的扩展中删除。
。根据实验数据重新排序条件,以便最常见的
true
比较首先,第二个最频繁的true
比较是第二等。使用
time.time()
来测量执行时间是不可靠的。此方法将不考虑其他过程使用的时间。因此,如果在其他过程产生较高的资源争夺时进行测量,则测量将大于您尝试测量的执行时间。错误的另一个来源是,系统时间可能在执行过程中发生变化(很可能是由于NTP同步引起的)。这些是我建议使用Cprofile
的软件包进行分析的一些原因。编辑:
我注意到某些
sultil.move()
调用将文件从C驱动器移动到D驱动器。这可以触发实际的副本而不是重命名,这可能会导致运行时大幅增加,具体取决于您的文件系统的设置方式。The first thing to do is profile this code to find out where it spends the time. Optimize operations in this order:
#2 is a balancing act between the two types of ops, but generally frequent and short will be the better option.
If the majority of the time is spent in
shutil.move()
you'll be able to reduce the runtime by at most half, theoretically. Practically, you won't be able to reduce it by this much. In this case, multithreading is suitable because the operation is I/O-bound.Absent profile information, the following is the best I can do.
The easiest way to improve speed is to change
docs
,imgs
andprgms
toset
S. Typically, aset
is faster than alist
for collections with more than two elements.Another opportunity lies in using
str.rsplit()
instead of slicing to get the file extension. A slice is a new string and this code does it multiple times per loop. Do it once at the start of the loop before the conditional block. This will require removing the.
s from the extensions indocs
,imgs
andprgms
.Reorder your conditional based on experimental data so that the most frequently
True
comparison is first, the second most frequentlyTrue
comparison is second and so forth.Using
time.time()
to measure execution time is unreliable. This method will not take into account time used by other processes. So, if measuring while other processes are generating high resource contention, the measurement will be greater than the execution time of what you're attempting to measure. Another source of error is that the system time may change during execution (most likely due to a NTP synchronization). These are some of the reasons I recommend profiling with a package likecProfile
.Edit:
I noticed that some of the
shutil.move()
calls move files from the C drive to the D drive. This can trigger an actual copy rather than rename which can result in a significant increase in runtime, depending on how your filesystems are set up.