本文谈如何用chatgpt解决工作中的实际问题。
方法很简单:把自己的诉求告诉chatgpt,让它生成实现自动化操作的python代码(公司允许装python)。
我这个诉求很简单,之前曾麻烦一位懂python的it部同事写代码,结果周期长(毕竟别人也有工作),而且代码好像也跑不起来——倒不是对方水平差,主要还是不了解我这边的具体局域网环境。后来他也离职了。而把我的需求输入搜索引擎,并不能得到有效的代码。
我的诉求是:把当前文件夹里符合条件(包含关键词)的pdf文档另存到局域网的某个目标文件夹——在公司内部,这种文件归类保存的需求还是挺多的。
之前麻烦别人写代码,至少得打好几个电话,然后等很久。有了chatgpt,把需求直接丢给它就行。
很快代码就来了(如下)
import os
import shutil# define the keywords to search for in the file names
keywords = [“xxxxx讯”]# define the source and destination directories
src_dir = “\\*******\\userdata\\*******5/rf/desktop\\working folder\\*******” # current directory
dst_dir = “\\*******t\\groupshares\\*******\\*******\\*******/*******/daily”# iterate through all the files in the source directory
for file_name in os.listdir(src_dir):
# check if the file name contains any of the keywords
if any(keyword in file_name for keyword in keywords):
# check if the file is a pdf
if file_name.endswith(“.pdf”):
# construct the full file path
src_path = os.path.join(src_dir, file_name)
dst_path = os.path.join(dst_dir, file_name)
# copy the file to the destination directory
shutil.copy2(src_path, dst_path)就这么简单。当然,之后还可以继续叠加其他归类规则。
只是有一点需要特别注意:我们的系统是win10架构,文件或目录的绝对路径里都包含\\反斜杠;而python会把反斜杠认作转义符,导致出错(这是报错后我搜网络得出的结论)。解方就是把所有反斜杠转为正斜杠/即可。
最后,说几句题外话:chatgpt并不能帮你有效完成作业,因为它的答案经常包含错误,甚至是离了大谱的谬误(之后我还会写一篇专栏来介绍),所以那种将来所有学生用chatgpt写作业糊弄老师的现象,至少目前并不会出现——除非老师也是糊涂蛋。
但让chatgpt生成代码就好得多,因为代码能不能用,一run便知。而且把代码出错讯息丢给ai,也能得到比较合理的回答,chatgpt等于是你在学习编程路上的一个小”导师“。虽然不是全知全能,但已经很不错了。