python-3.x - why is Windows is truncating the name of a file I create using open() in python 3.6?
问题描述
I am attempting to port a python 3.6 program from Ubuntu 18 to Windows 10. In this file I am creating a long string with a date time object in it and using it as a filename. The open() command works fine in ubuntu and creates a long string ending with the ".txt" However Windows 10 does not like this and truncates the filename to 32 bits (I'm not sure if that is on purpose or coincidence.) The code is the same but the operating systems treat if differently. Here is the code:
# Capture the date time object format to write to log file
datetime_object = datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
print(datetime_object)
Error_file_name = "ESP32_Company_Error_log_" + datetime_object + ".txt"
print(Error_file_name)
Info_file_name = "ESP32_Company_Information_log_" + datetime_object + ".txt"
#Open the files to write
err_fl = open(Error_file_name, "a")
#print(type(err_fl))
info_fl = open(Info_file_name, "a")
Windows will create a generic file
ESP32_Company_Information_log_11
Which is not a .txt file and therefor not open it later on. Ubuntu creates "ESP32_Company_Information_log_11:15AM on May 14, 2019.txt"
I've tried import os, did not work. I've looked at the type and cannot figure out why a text IO wrapper would have this.
解决方案
看来日期时间格式很重要 rAndom69 你是正确的。更改格式解决了这个问题。
推荐阅读
- java - 我可以要求泛型参数具有泛型参数吗?
- c - 调用另一个函数如何填充 C 中数组的元素?
- css - 当我使用 col-* 并占据全宽时,引导程序没有响应,尽管它在大屏幕上工作?
- javascript - 使用javascript更改子元素的innerHTML
- jenkins - Jenkins 插件开发 - 如何检查插件是否在 Jenkins Master 上执行
- grails - grails 3.x中grails 2.x的Config.groovy对应的神器是什么?
- regex - 多个正则表达式匹配不是 Haskell 中的 [String]
- python - 对特定列 python 取每一行的正弦值
- python - 如何通过登录 Seaborn 等量缩放 x 和 y 轴?
- c# - 正则表达式模式不在 c# 中工作,但在在线正则表达式测试器中工作