input文件选择控件,python input 文件路径_python – 将目录路径作为用户输入的正确方法是什么?

 2023-09-22 阅读 34 评论 0

摘要:下面是我尝试用来将目录路径作为用户的“原始输入”的代码片段.从用户输入后,我收到以下错误:Traceback (most recent call last):File "C:\Users\larece.johnson\Desktop\Python Programs\Hello World 2", line 14, in input文件选择控件、f = open(str,&

下面是我尝试用来将目录路径作为用户的“原始输入”的代码片段.从用户输入后,我收到以下错误:

Traceback (most recent call last):

File "C:\Users\larece.johnson\Desktop\Python Programs\Hello World 2", line 14, in

input文件选择控件、f = open(str,"r+") #I open the text file here which the user gave me

IOError: [Errno 2] No such file or directory: 'C:/Users/larece.johnson/Desktop/Python Programs/BostonLog.log.2014-04-01'

忽略我在下面所做的,是否有一种特殊的方式我应该从用户那里获取路径,以便Python接受它?

例如,我正在寻找的目录和文件是

python raw_input。C:/Users/larece.johnson/Desktop/Python Programs/BostonLog.log.2014-04-01

import re #this library is used so that I can use the "search" function

import os #this is needed for using directory paths and manipulating them

str ="" #initializing string variable for raw data input

python 2input,#print os.getcwd()

#f = open("C:/Users/larece.johnson/Desktop/BostonLog.log.2014-04-02.log","r+")

str = raw_input("Enter the name of your text file - please use / backslash when typing in directory path: "); #User will enter the name of text file for me

f = open(str,"r+")

python中input的用法?解决方法:

我想你应该尝试类似的东西:

import sys

import os

python 路径。user_input = raw_input("Enter the path of your file: ")

assert os.path.exists(user_input), "I did not find the file at, "+str(user_input)

f = open(user_input,'r+')

print("Hooray we found your file!")

python str。#stuff you do with the file goes here

f.close()

标签:raw-input,python

来源: https://codeday.me/bug/20191006/1857970.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/82573.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息