1

I have written the code for automation of creating file with date and time format. So, the file output would be

Product_Name-date

From this code:

filename = datetime.now().strftime('Product_Name-%Y-%m-%d-%H-%M.csv')
with open(filename, "w+") as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(["Name", "Price"])
    for d in datas: csv_output.writerow(d)

However, I want to ask for input from user to manually name each file. To become:

UserInputProduct_Name-date

I've read the tutorial from W3School, but I can't implement it correctly.

 filename = input("Input the Filename: ")
 f_extns = filename.split(".")
 print ("The extension of the file is : " + repr(f_extns[-1]))

How do I do that?

  • What exactly is the problem? `f_extns[-1]` *is* the (last) extension, assuming the file name has an extension. – chepner Jul 03 '21 at 14:23
  • You probably don't want a full file name, just a "stem" to which you'll append the date and `.csv`: `stem = input(); filename = datetime.now().strftime(f'{stem}-%Y-%m-%d-%H-%M.csv')`. – chepner Jul 03 '21 at 14:25
  • You can also just strip `.csv` from the user input if you are worried about it being duplicated. – chepner Jul 03 '21 at 14:26
  • Sorry, I'm not too familiar with the syntax, currently learning Python from scratch. Thanks for the help guys. – Muhammad Yusril Nur Ramadhan Jul 03 '21 at 14:31

3 Answers3

0

Is that what you want?

Product_Name = input("Input the Filename: ")
filename = datetime.now().strftime(f'{Product_Name}-%Y-%m-%d-%H-%M.csv')
with open(filename, "w+") as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(["Name", "Price"])
    for d in datas: csv_output.writerow(d)
bera TV
  • 34
  • 2
0

I am not sure if I got your question right, but here is what I think you are looking for.

Product_Name = input("Enter filename: ")
filename = datetime.now().strftime('{}-%Y-%m-%d-%H-%M.csv').format(Product_Name)
with open(filename, "w+") as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(["Name", "Price"])
    for d in datas:
        csv_output.writerow(d)

After testing this code myself, I got file named:

test-2021-07-03-16-30.csv

Dharman
  • 30,962
  • 25
  • 85
  • 135
kesetovic
  • 144
  • 6
0

As bera TV have answered, that is the solution according to your First question

I assume that your second question is to "get the extention of a file"

Which is a possible duplicate of this question

As a Solution, you can use the pathlib avilable in python 3.x

>>> import pathlib
>>> x = pathlib.PurePosixPath("C:\\Path\\To\\File\\myfile.exten").suffix
>>> print(x)
".exten"
imamangoo
  • 61
  • 9
  • Hi. I suppose my question might be confusing. But, I'm trying to ask for user input to name the file, which Bera TV and mortex already answered correctly according to my needs. Anyway, thanks for the answer. Might need that later in the future. – Muhammad Yusril Nur Ramadhan Jul 03 '21 at 14:50
  • Great then, I thought your second code was to ask for the extension as you are accessing the last element using [-1]. Sorry for the misunderstanding, Have a nice day – imamangoo Jul 03 '21 at 14:56
  • I'm sorry for the confusion too. I'm very new to Python, so I might not understand some syntax that I'm using. After all, everything that I've learned might have resulted from trial and error without fully knowing the syntax. Cheers! – Muhammad Yusril Nur Ramadhan Jul 03 '21 at 15:15