2

am having a code to create a csv file and can upload it to ftp server

with open(csv_file, 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=csv_columns1) #csv_columns1 is a list of value to become  as heading
    writer.writeheader()
    for data in vals['details']:# values for the header
        writer.writerow(data)
    writer = csv.DictWriter(csvfile, fieldnames=csv_columns2)#csv_columns1 is a list of value to become  as heading
    writer.writeheader()
    writer = csv.DictWriter(csvfile, fieldnames=csv_columns)#csv_columns1 is a list of value to become  as heading
    writer.writeheader()
    for data in vals['OrderLine']:# values for the header
        writer.writerow(data)
        print(os.system('pwd'))

     Output_Directory = "ftp_path_to_store_file"
     username = "ftp_user_names"
     password = "ftp_password"
     ftp_ip = "ftp_host_ip"
     a1 = 'STOR %s.csv' % (order.name)

     try:
        ftp = FTP(ftp_ip)
        ftp.login(username, password)
        ftp.cwd(Output_Directory)
        with open(csv_file, "rb") as f:
             ftp.storbinary('STOR ' + os.path.basename(csv_file), f)

but what i need was without creating a file on my computer i want to create a file directly to ftp server

Any help would be appreciated

7
  • Don't write it to a file... write it to a io.BytesIO object... and then give ftp.storbinary that object... Commented Sep 19, 2018 at 12:44
  • i tried it too. but it shows me some error like writer object has no attribute DictWriter. can you please show me how can i do that. with the above code csv_column1=[head1, head2,head3] csv_column2=[head21, head22,head23] ..etc Commented Sep 19, 2018 at 12:45
  • Why are you creating multiple CSV writers btw? Commented Sep 19, 2018 at 12:46
  • am having multiple lines of heading to add to csv is there any other way to do that Commented Sep 19, 2018 at 12:48
  • That's not how CSV files work... but anyway... your code might just work by changing with open(csv_file, 'w') as csvfile: to with io.BytesIO() as csvfile... Commented Sep 19, 2018 at 12:49

1 Answer 1

5

You can make the csv writer write to a io.StringIO object instead and then convert the output to an io.BytesIO object after encoding its text value to bytes:

import io
csvfile = io.StringIO()
writer = csv.DictWriter(csvfile, fieldnames=csv_columns1) #csv_columns1 is a list of value to become  as heading
writer.writeheader()
for data in vals['details']:# values for the header
    writer.writerow(data)
writer = csv.DictWriter(csvfile, fieldnames=csv_columns2)#csv_columns1 is a list of value to become  as heading
writer.writeheader()
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)#csv_columns1 is a list of value to become  as heading
writer.writeheader()
for data in vals['OrderLine']:# values for the header
    writer.writerow(data)
    print(os.system('pwd'))

Output_Directory = "ftp_path_to_store_file"
username = "ftp_user_names"
password = "ftp_password"
ftp_ip = "ftp_host_ip"
a1 = 'STOR %s.csv' % (order.name)

try:
    ftp = FTP(ftp_ip)
    ftp.login(username, password)
    ftp.cwd(Output_Directory)
    ftp.storbinary('STOR ' + os.path.basename(csv_file), io.BytesIO(csvfile.getvalue().encode()))
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - Though, while this works, it can be pretty inefficient, if the file becomes large. For more efficient (while more complicated) solution, see Python - Upload a in-memory file (generated by API calls) in FTP by chunks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.