<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-CN">
    <title>IT Notes - python</title>
    <subtitle>Personal notes on software engineering, systems</subtitle>
    <link rel="self" type="application/atom+xml" href="https://blog.1192929.xyz/tags/python/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://blog.1192929.xyz"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2025-01-15T00:00:00+00:00</updated>
    <id>https://blog.1192929.xyz/tags/python/atom.xml</id>
    <entry xml:lang="zh-CN">
        <title>我的第二篇 IT 笔记：Python 小工具</title>
        <published>2025-01-15T00:00:00+00:00</published>
        <updated>2025-01-15T00:00:00+00:00</updated>
        
        <author>
          <name>刘林浩</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.1192929.xyz/posts/second-post/"/>
        <id>https://blog.1192929.xyz/posts/second-post/</id>
        
        <content type="html" xml:base="https://blog.1192929.xyz/posts/second-post/">&lt;p&gt;记录一个常用的 Python 小工具：批量给目录下的文件添加日期前缀。它只依赖标准库，任何 Python 3.6+ 环境都能直接运行。&lt;/p&gt;
&lt;h2 id=&quot;jiao-ben&quot;&gt;脚本&lt;a class=&quot;zola-anchor&quot; href=&quot;#jiao-ben&quot; aria-label=&quot;Anchor link for: jiao-ben&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;import&lt;/span&gt;&lt;span&gt; pathlib&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;import&lt;/span&gt;&lt;span&gt; datetime&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; add_date_prefix&lt;/span&gt;&lt;span&gt;(directory: pathlib.Path) -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; int&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;Add a YYYY-MM-DD prefix to every regular file in the directory.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    today&lt;/span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;/span&gt;&lt;span&gt; datetime.date.today().isoformat()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    renamed&lt;/span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color: #FFAB70;&quot;&gt; file&lt;/span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;/span&gt;&lt;span&gt; directory.iterdir():&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color: #FFAB70;&quot;&gt; file&lt;/span&gt;&lt;span&gt;.is_file()&lt;/span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; and not&lt;/span&gt;&lt;span style=&quot;color: #FFAB70;&quot;&gt; file&lt;/span&gt;&lt;span&gt;.name.startswith(today):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFAB70;&quot;&gt;            file&lt;/span&gt;&lt;span&gt;.rename(&lt;/span&gt;&lt;span style=&quot;color: #FFAB70;&quot;&gt;file&lt;/span&gt;&lt;span&gt;.with_name(&lt;/span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;{&lt;/span&gt;&lt;span&gt;today&lt;/span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;_&lt;/span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #FFAB70;&quot;&gt;file&lt;/span&gt;&lt;span&gt;.name&lt;/span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            renamed&lt;/span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +=&lt;/span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; renamed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; __name__&lt;/span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;__main__&amp;quot;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;    print&lt;/span&gt;&lt;span&gt;(add_date_prefix(pathlib.Path(&lt;/span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span&gt;)))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;shi-yong-bu-zou&quot;&gt;使用步骤&lt;a class=&quot;zola-anchor&quot; href=&quot;#shi-yong-bu-zou&quot; aria-label=&quot;Anchor link for: shi-yong-bu-zou&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;把上面的脚本保存为 &lt;code&gt;prefix_dates.py&lt;/code&gt;。&lt;/li&gt;
&lt;li&gt;进入目标目录，运行 &lt;code&gt;python prefix_dates.py&lt;/code&gt;。&lt;/li&gt;
&lt;li&gt;检查输出：脚本会返回本次重命名的文件数量。&lt;/li&gt;
&lt;li&gt;如需回滚，把文件名开头的日期前缀删除即可。&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;shuo-ming&quot;&gt;说明&lt;a class=&quot;zola-anchor&quot; href=&quot;#shuo-ming&quot; aria-label=&quot;Anchor link for: shuo-ming&quot;&gt;🔗&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;脚本跳过已经带当天前缀的文件，重复执行是幂等的。&lt;/li&gt;
&lt;li&gt;依赖零第三方库，Python 标准库即可满足。&lt;/li&gt;
&lt;li&gt;本文归入 &lt;code&gt;python&lt;/code&gt; 与 &lt;code&gt;tooling&lt;/code&gt; 两个标签，便于归档检索。&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
</feed>
