import sys import os class Notepad() x = str() y = str() def save(self): print('''Введите путь файла для копирования - c:\\''afs''\\''asdffads''\\''корректный путь''') text1 = sys.stdin.readline() if text1 == ('Здесь должен быть указан корректный путь, если он корректный то открывает этот путь, например \\с:\\user\\итд, как сделать'): for text1 in range(''): #здесь ошибка, не знаю как сделать x = open(text1, 'r') filetext1 = x.read() else: print('Выводит сообщение что неправильно ввели путь и возвращает на строку 7-8, как сделать') print('''Введите путь файла для создания, в который хотите скопировать - c:\\''afs''\\''asdffads''\\''корректный путь''') text2 = sys.stdin.readline() if text2 == ('Здесь должен быть указан корректный путь, если он корректный то открывает этот путь, например \\с:\\user\\итд, как сделать'): for text2 in range (''): #здесь ошибка, не знаю как сделать y = open(text2, 'w') text2 = y.write(filetext1) x.close() y.close() y = open(text2, 'r') filetext2 = y.read() print(filetext2) #выводит содержимое второго файла с информацией откопированной из первого else: print('Выводит сообщение что неправильно ввели путь и возвращает на строку 7-8, как сделать') 

Hello, I am learning to program. Tell me, please, I want to make a program that takes the value of the path of the first file, processes it, then takes the value of the path of the second file, which you need to create and copy into it the values ​​from the first. Text files. How to make the program understand the correct path to the file? How to make a return from a certain point of code to the desired one used earlier? I would like to leave the prog in this form, as I still do not understand other types. Modules can add others. Please correct the errors, thank you.

UPDATE:

 import os class Notepad: def open(): while True: firstfile = input('Введите путь файла для копирования ') if os.path.exists(firstfile): print('Копируем file'.format(firstfile)) text1 = open(firstfile, 'r') filetext1 = text1.read() text1.close() break else: print('Неудалось найти файл'.format(firstfile)) def create(): while True: secondfile = input('Создайте файл с необходимым путем') if os.path.exists(secondfile): print('Создаем новый файл'.format(secondfile)) text2 = open(secondfile, 'w') filetext2 = text2.write(filetext1) filetext2.close() break else: print('Неудалось создать файл'.format(secondfile)) def opennew(): while True: if os.path.exists(secondfile): text2 = open(secondfile, 'r') filetext2 = y.read() print(filetext2) break 

This code is made, designed in the form of classes and functions. I run into the python - all the rules. Then I call the open () function and get an error. Why is that? What to fix? What to read about?

 >>> open() Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> open() TypeError: Required argument 'file' (pos 1) not found 

    1 answer 1

     import os if __name__ == "__main__": while True: filepath_old = input( 'Введите путь файла для копирования: ' ) if os.path.exists( filepath_old ): print( 'Копируем содержимое файла: {}'.format( filepath_old ) ) file_old = open( filepath_old, 'r' ) text_old = file_old.read() file_old.close() break else: print( 'Не удалось найти файл: {}'.format( filepath_old ) ) while True: filepath_new = input( 'Введите путь файла в который хотите скопировать {}: '.format( filepath_old ) ) try: file_new = open( filepath_new, 'w' ) # 'w' - если файл существует, то будет очищен | 'a' - пишет в конец файла file_new.write( text_old ) file_new.close() print( 'Содержимое файла: {} сопированно в: {}'.format( filepath_old, filepath_new ) ) break except Exception as e: print( e ) exit() 

    UPDATE:

     import os class notepad: files = {} def __init__( self ): print( 'Какие файлы копируем?' ) while True: filepath = input( 'Укажите полное имя файла: ' ) r = self.new( filepath ) if r: print( 'Файл {} добавлен.'.format( filepath ) ) else: print( 'Не удалось найти файл: {}'.format( filepath ) ) r = input( 'Добавить еще один файл? ( Y / Д / 1 | N / Н / 0 ): ' ).lower() if r == 'y' or r == 'д' or r == 1: pass elif r == 'n' or r == 'н' or r == 0: break print( 'Куда копируем?' ) while True: filepath = input( 'Укажите полное имя файла: ' ) try: file = open( filepath, 'w' ) # 'w' - если файл существует, то будет очищен | 'a' - пишет в конец файла for i in self.files: file.write( '{}\n'.format( self.files[i].read() ) ) print( 'Содержимое файла: {} сопированно в: {}'.format( i, filepath ) ) self.files[i].close() file.close() break except Exception as e: print( e ) break def new( self, filepath ): if os.path.exists( filepath ): if not self.files.get( filepath ): self.files[filepath] = open( filepath, 'r' ) return True else: return False if __name__ == "__main__": notepad = notepad() 

    Your error arises from the fact that you are trying to open a file, and not to call a function from the Notepad class. In order to call a function from a class, it must be declared.

     имя_переменной = имя_класса() имя_переменной.имя_функции() 
    • thanks, very cool! - MaKaroNNiK
    • which book taught you to navigate so quickly? - MaKaroNNiK
    • Did you write this code yourself? Can you please, if you do not mind, to describe the course of your thoughts in addressing this issue. - MaKaroNNiK