Command prompt says unable to find file when file is there

1.4k Views Asked by At

So I have a .py script that I am trying to run, and in either command prompt or anaconda prompt. If I run python filename.py, it gives me this error: python: can't open file 'C:\Users\tform\Documents\AirSim\CapstoneAPI.py': [Errno 2] No such file or directory.

I am not sure how to solve this issue because the file is in that location. Do I need to mess around with some settings?

I am using Visual Studio 2019 as my IDE for python.

    import os
import os.path

import csv


import datetime
import airsim

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

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


'''
Create base format for file names that specify date and time of run
'''
run_date_and_time = datetime.datetime.now()
run_date_and_time_string = run_date_and_time_string = run_date_and_time.strftime('%y-%m-%d_%H%M%S')
extension = ".csv"
file_name_base = run_date_and_time_string + extension

'''
Create each sensor type file names and join then to savepath
'''
imu = "imu"
gps = "gps"
magnetometer = "magnetometer"
barometer = "barometer"

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

imu_file_name = imu + file_name_base
imu_ouput_file = os.path.join(dirmain,imu_file_name)

mag_file_name = magnetometer + file_name_base
mag_ouput_file = os.path.join(dirmain,mag_file_name)

bar_file_name = barometer + file_name_base 
bar_output_file = os.path.join(dirmain,bar_file_name)



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





while True: 
  

    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(output_file,'a') as gpscsvfile:
       gpscsvwriter = csv.writer(gpscsvfile)
       gpscsvwriter = gpscsvwriter.writerow(gps_data_struct)    

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

    #print("Altitude: %s\nLatitude %s\nLongitude %s" %(alt,lat,lon) )
    if False:
      break
1

There are 1 best solutions below

0
On BEST ANSWER

As people said, when we use the command "python name.py" in the terminal to run a python script, we need to enter the parent folder of this python file so that it can be found.

enter image description here

Solution: We could use "cd" to go to the parent folder of the python file: (Please go to your folder "AirSim".)

enter image description here