首页 > 解决方案 > 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.

标签: python-3.xwindowsubuntu

解决方案


看来日期时间格式很重要 rAndom69 你是正确的。更改格式解决了这个问题。


推荐阅读