Python 3.x 如何替换和重命名为同一变量
我怎样才能把它缩短成一行呢Python 3.x 如何替换和重命名为同一变量,python-3.x,Python 3.x,我怎样才能把它缩短成一行呢 latest_dir = "test for test" new_name = latest_dir.replace(' ', '_') os.rename(latest_dir, new_name) latest_dir = new_name 我想从中重命名目录 test for test 到 但它应该保持变量的最新\u dir 关于在Python 3.8+中,您可以使用赋值表达式在一行中完成: os.rename(latest_dir, (latest_di
latest_dir = "test for test"
new_name = latest_dir.replace(' ', '_')
os.rename(latest_dir, new_name)
latest_dir = new_name
我想从中重命名目录
test for test
到
但它应该保持变量的最新\u dir
关于在Python 3.8+中,您可以使用赋值表达式在一行中完成:
os.rename(latest_dir, (latest_dir := latest_dir.replace(' ', '_')))
在Python 3.8+中,可以使用赋值表达式在一行中完成:
os.rename(latest_dir, (latest_dir := latest_dir.replace(' ', '_')))