命令提示符说文件存在时无法找到文件
Command prompt says unable to find file when file is there
所以我有一个 .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 作为 IDE 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
正如人们所说,当我们在终端中使用命令“python name.py
”到运行一个python脚本时,我们需要进入这个[=21的父文件夹=] 文件,以便可以找到它。
解决方案:我们可以使用“cd”进入python文件的父文件夹:(请进入您的文件夹“AirSim”。)
所以我有一个 .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 作为 IDE 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
正如人们所说,当我们在终端中使用命令“python name.py
”到运行一个python脚本时,我们需要进入这个[=21的父文件夹=] 文件,以便可以找到它。
解决方案:我们可以使用“cd”进入python文件的父文件夹:(请进入您的文件夹“AirSim”。)