Python 自动给重名文件名添加序号

检查给定的文件路径是否已经重名,并自动添加序号,递增数字。

Code on Github Gist

def auto_rename_exist_filename(file_path):
    """
    For example: a.txt, a(1).txt, a(2).txt
    Source: https://gist.github.com/nodewee/9ae9b0b461b4bcaf30bc2b84ca8c4743.js
    """
    if not os.path.exists(file_path):
        return file_path

    dir_path = os.path.dirname(file_path)
    src_filename = os.path.basename(file_path)
    [file_title, ext_name] = os.path.splitext(src_filename)

    pattern = r"\((\d+?)\)$"
    r = re.search(pattern, file_title)
    if r:
        serial = str(int(r.groups()[0]) + 1)
        new_title = re.sub(pattern, "(" + serial + ")", file_title)
    else:
        new_title = file_title + "(1)"

    new_filepath = os.path.join(dir_path, new_title + ext_name)

    if os.path.exists(new_filepath):
        return auto_rename_exist_filename(new_filepath)
    else:
        return new_filepath

Python 解决 Windows 下 PyCrypto 出错的正确方法

如何简单又可靠的解决 Windows 下 PyCrypto 报错。

直接 pip install pycrypto 安装该第三方库时,
或使用 PyInstaller 的加密参数时,
都可能会遇到 pycrypto 报错,通常是 cl.exe failed with exit status 2

网上很多人遇到这个问题,也有很多文章讨论和给出解决办法。在我两次遇到这个头疼的问题之后,

Excel或WPS 锁定单元格

两个步骤:
1,设置单元格属性为“锁定”
2,开启工作表保护

选中要锁定的单元格区域,右键选择打开「设置单元格格式」,或快捷键ctrl+1(Mac是 ⌘cmd+1)。
「保护」选项卡,选中「锁定」

「审阅」菜单,点击「保护工作表」,输入密码并根据情况勾选调整权限,确定,完成。

/attachments/e3c2f3145995957977f3f096d3f183c5.png

此时,工作表名称前面会多处一个黑色的小锁🔒图标。

Python 获取当前的操作系统

获取当前是什么操作系统

环境:Python 3

  • 使用 platform.system()
import platform
print(platform.system())
import platform
isWindows = True if platform.system() == 'Windows' else False
isMacOS = True if platform.system() == 'Darwin' else False
  • 使用 sys.platform
import sys
print(sys.platform)

# darwin # is macOS
# win32 # is Windows