【问题标题】:Command prompt says unable to find file when file is there命令提示符说文件存在时找不到文件
【发布时间】:2021-02-23 20:45:09
【问题描述】:

所以我有一个我正在尝试运行的 .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

【问题讨论】:

  • 你能把你的代码和你的目录也包括进来吗?
  • 不提供完整的填充路径时,文件需要在当前目录下。如果您使用的是 IDE,它可能不是您想象的那样,您可以通过 print(os.getcwd()) 看到当前工作目录是什么。
  • @Shubham Nipanikar - 进展如何?只需检查一下提供的信息是否有帮助。

标签: python visual-studio-code command-line airsim


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-12-08
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2017-08-30
    相关资源
    最近更新 更多