首页 > 解决方案 > 命令提示符说文件存在时无法找到文件

问题描述

所以我有一个 .py 脚本,我试图在命令提示符或 anaconda 提示符下运行。如果我运行python filename.py,它会给我这个错误: python: can't open file 'C:\Users\tform\Documents\AirSim\CapstoneAPI.py': [Errno 2] No such file or directory

我不确定如何解决此问题,因为该文件位于该位置。我需要搞乱一些设置吗?

我使用 Visual Studio 2019 作为我的 Python IDE。

    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

标签: pythonvisual-studio-codecommand-lineairsim

解决方案


正如人们所说,当我们python name.py在终端中使用“”命令运行一个python脚本时,我们需要进入这个python文件的父文件夹才能找到它。

在此处输入图像描述

解决方案:我们可以使用“cd”进入python文件的父文件夹:(请进入你的文件夹“AirSim”。)

在此处输入图像描述


推荐阅读