如何缩短它并排成一行?
latest_dir = "test for test"
new_name = latest_dir.replace(' ', '_')
os.rename(latest_dir, new_name)
latest_dir = new_name
我想从重命名目录
test for test
至
test_for_test
但应保留变量last_dir
问候
在Python 3.8+中,您可以使用赋值表达式在一行中完成此操作:
os.rename(latest_dir, (latest_dir := latest_dir.replace(' ', '_')))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句