Можете ли вы получить необработанную строку с помощью input(), чтобы Python правильно открывал текстовые файлы?Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Можете ли вы получить необработанную строку с помощью input(), чтобы Python правильно открывал текстовые файлы?

Сообщение Anonymous »


I'm trying to make a python script (PyCharm, python version 3.9) which you can write and read any document on my laptop, however, the backslashes in the path of the text file are being read incorrectly when i use it as:

global file def start(): global file print("text file:") file = input() print("type:") m = input() if m == 'r': read() def read(): with open(file, 'a+') as f: f.seek(0) print(f.read()) the console when i run it is:

script >> text file: me >> "C:\Users\mylan\Downloads\test.txt" script >> type: me >> r
Изображение


The error I'm facing is:

OSError: [Errno 22] Invalid argument: '"C:\\Users\\mylan\\Downloads\\test.txt"' I have already searched for related questions and answers but could only find a solution using a r in front:

file = r"C:\Users\mylan\Downloads\test.txt The problem with this, however, is that the file that is read is set in code instead of coming from the input, so, is it possible to get the effect of using r"C:..." while using input() ?


Источник: https://stackoverflow.com/questions/780 ... -text-file
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»