How to create a file name with the current date & time in Python?

后端 未结 6 1371
北海茫月
北海茫月 2020-12-07 07:40

Here is a functional code (create file with success)

sys.stdout = open(\'filename1.xml\', \'w\')

Now I\'m trying to name the file with the cu

6条回答
  •  清歌不尽
    2020-12-07 08:36

    Here's some that I needed to include the date-time stamp in the folder name for dumping files from a web scraper.

    # import time and OS modules to use to build file folder name
    import time
    import os 
    
    # Build string for directory to hold files
    # Output Configuration
    #   drive_letter = Output device location (hard drive) 
    #   folder_name = directory (folder) to receive and store PDF files
    
    drive_letter = r'D:\\' 
    folder_name = r'downloaded-files'
    folder_time = datetime.now().strftime("%Y-%m-%d_%I-%M-%S_%p")
    folder_to_save_files = drive_letter + folder_name + folder_time 
    
    # IF no such folder exists, create one automatically
    if not os.path.exists(folder_to_save_files):
        os.mkdir(folder_to_save_files)
    

提交回复
热议问题