Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python脚本的文件名中添加当前日期_Python_Python 3.x_Snowflake Cloud Data Platform - Fatal编程技术网

如何在python脚本的文件名中添加当前日期

如何在python脚本的文件名中添加当前日期,python,python-3.x,snowflake-cloud-data-platform,Python,Python 3.x,Snowflake Cloud Data Platform,我试图将数据从雪花卸载到GCS,为此我使用了雪花python连接器和python脚本。在以下文件名为“LH_TBL_FIRST20200908”的python脚本中,如果脚本今天运行,则名称相同;如果脚本明天运行,则文件名应为“LH_TBL_FIRST20200909”,同样,如果它在第二天运行,则文件名应为“LH_TBL_FIRST202009010” 另外,请告诉我代码中是否有任何错误。代码如下 import snowflake.connector # Gets

我试图将数据从雪花卸载到GCS,为此我使用了雪花python连接器和python脚本。在以下文件名为“LH_TBL_FIRST20200908”的python脚本中,如果脚本今天运行,则名称相同;如果脚本明天运行,则文件名应为“LH_TBL_FIRST20200909”,同样,如果它在第二天运行,则文件名应为“LH_TBL_FIRST202009010”

另外,请告诉我代码中是否有任何错误。代码如下


    import snowflake.connector
    
    # Gets the version
    ctx = snowflake.connector.connect(
        user='*****',
        password='*******',
        account='********',
        warehouse='*******',
        database='********',
        schema='********'
        )
    cs = ctx.cursor()
    
    sql = "copy into @unload_gcs/LH_TBL_FIRST20200908.csv.gz
     from ( select * from TEST_BASE.LH_TBL_FIRST )
     file_format = 
     ( type=csv compression='gzip' 
     FIELD_DELIMITER = ',' 
     field_optionally_enclosed_by='"'
     NULL_IF=()
     EMPTY_FIELD_AS_NULL = FALSE
     ) 
     single = fals
     e max_file_size=5300000000
     header = false;"
    cur.execute(sql)
    
    cur.close()
    conn.close()

您可以使用来填写(部分)文件名。Python具有处理日期和时间的模块

from datetime import datetime

date = datetime.now().strftime('%Y%m%d')
myFileName = f'LH_TBL_FIRST{date}.csv.gz'

print(myFileName)
>>> LH_TBL_FIRST20200908.csv.gz
至于代码中的错误: 您将光标声明为
ctx.cursor()
,接下来只需使用
cur.execute(…)
cur.close(…)
。这些不行。运行代码以查找错误并修复它们

编辑建议由@Lysergic:

如果您的python版本太旧,可以使用


我采取以下方法

#defining what time/date related values your variable will contain
date_id = (datetime.today()).strftime('%Y%m%d')
写入输出文件

#Creating the filename 
with open(date_id + "_" + "LH_TBL.csv.gz" 'w') as gzip:
输出:YYYY/MM/DD_uu文件名

20200908_filename

嗨,欢迎来到苏。请不要叫我们快点,我们不欠你任何东西。请带上这张桌子,熟悉一下这里的道德规范。
20200908_filename
from datetime import datetime


class FileNameWithDateTime(object):
    def __init__(self, fileNameAppender, fileExtension="txt"):
        self.fileNameAppender = fileNameAppender
        self.fileExtension = fileExtension
   
    def appendCurrentDateTimeInFileName(self,filePath):
        currentTime = self.fileNameAppender
        print(currentTime.strftime("%Y%m%d"))
        filePath+=currentTime.strftime("%Y%m%d")
        filePath+="."+self.fileExtension
        try:
            with open(filePath, "a") as fwrite1:
                fwrite1.write(filePath)    
                
        except OSError as oserr:
                print("Error while writing ",oserr)