Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

python - CSV File not showing up at file location

So I am writing a script that runs when a simulation running in AirSim runs. It is continuously collecting data (theoretically) and writing it to the csv file. However, when I go to the location that it should be saved, there is no file. Like even if there is an error in my method of saving the data, the file itself is not there.

import setup_path
import airsim


import numpy as np
import os
import os.path
import string
import csv
import tempfile
import pprint
import csv
import datetime

client = airsim.MultirotorClient()
client.confirmConnection()
'''
First create a directory for all the csv files to store into.
''' 

dirmain = r"C:AirSimData"
if not os.path.exists(dirmain):
    os.mkdir(dirmain)


'''
Create format for file names
'''
run_date_and_time = datetime.datetime.now()
run_date_and_time_string = str(run_date_and_time)
extension = ".csv"
file_name_base = run_date_and_time_string + extension

imu = "imu"
gps = "gps"
magnetometer = "magnetometer"
barometer = "barometer"

gps_file_name = gps + file_name_base

'''Create csv files
'''
gps_file = open(r"gps_file_name",'w')   

gps_header = ['lat','lon','alt']
with open(r"gps_file_name",'w') as gpscsvfile:
    gpscsvwriter = csv.writer(gpscsvfile)
    gpscsvwriter = gpscsvwriter.writerow(gps_header)    

gpscsvfile.close()



while True: 
  

    #state = client.getMultirotorState()
    #s = pprint.pformat(state)
    #print("state: %s" % s)
    
   #imu_data = client.getImuData()
    #s = pprint.pformat(imu_data)
    #print("imu_data: %s" % s)

    #barometer_data = client.getBarometerData()
    #s = pprint.pformat(barometer_data)
    #print("barometer_data: %s" % s)

    #magnetometer_data = client.getMagnetometerData()
    #s = pprint.pformat(magnetometer_data)
    #print("magnetometer_data: %s" % s)

    gps_data = client.getGpsData().gnss.geo_point
    alt = (gps_data.altitude)
    lat = (gps_data.latitude)
    lon = (gps_data.longitude)
    gps_data_struct = [lat,lon,alt]
    
    with open(r"gps_file_name",'w') as gpscsvfile:
        gpscsvwriter = csv.writer(gpscsvfile)
        gpscsvwriter = gpscsvwriter.writerow(gps_data_struct)    

    gpscsvfile.close()

    #print("Altitude: %s
Latitude %s
Longitude %s" %(alt,lat,lon) )
    if False:
        break
question from:https://stackoverflow.com/questions/65835938/csv-file-not-showing-up-at-file-location

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here you are creating a file name literally "gps_file_name"

with open(r"gps_file_name",'w') as gpscsvfile:
    gpscsvwriter = csv.writer(gpscsvfile)
    gpscsvwriter = gpscsvwriter.writerow(gps_header)   

You should instead use the variables with the name elements that you created. os.path.join() is a safe way to join filenames with path names.

gps_file_name = gps + file_name_base    
output_file = os.path.join(dirmain, gps_file_name)

# Should read something like this  "C:AirSimDatagps2021-01-21 13:37:39.867152.csv"

Then you can use it here.

with open(output_file,'w') as gpscsvfile:
    gpscsvwriter = csv.writer(gpscsvfile)
    gpscsvwriter = gpscsvwriter.writerow(gps_header)    

gpscsvfile.close()

The next problem is that your datetime string contains invalid characters for filename colons (:) can not be used in file names. so you need to re-think that part.

One option could be to use no colons and have your time look like this.

run_date_and_time_string = run_date_and_time.strftime('%y-%m-%d_%H%M%S')
# 'C:\AirSimData\gps21-01-21_134531.csv'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...