По разделам

 

Решение задачи File 31


Дан файл целых чисел, содержащий более 50 элементов. Уменьшить его размер до 50 элементов, удалив из файла необходимое количество начальных элементов.

Код (Python)

# -*- coding: utf-8 -*-
import random
import sys

def GenerateNumbers(fname):
    N = random.randint(50,70)
    print("N = ",N)
    try:
        f = open(fname, "w")
        for i in range(0,N):
            #x = random.randrange(1,10)
            x = i + 1
            line = str(x)+"\n"
            f.write(line)
        f.close()
    except:
        print("Error: ", sys.exc_info()[0])

def CountFile(fname):
#count number of rows in given text-file 
    N = 0
    try:
        with open(fname,'r') as f:
            for line in f:
                N += 1
                #print(N,":",line)
    except IOError:
        print("Open error:",fname)
        return -1
    finally:
        return N

f_input = "file31_in.txt"
f_output = "file31_out.txt"
GenerateNumbers(f_input)
print("Read from:",f_input)
print("Write to:",f_output)

try:
    num_items = CountFile(f_input)
    if num_items <= 50:
        print("File must contain more than 50 items")
        sys.exit()

    num_extra = num_items - 50
    
    with open(f_input, 'r') as f_in, open(f_output, 'w') as f_out:
        for i in range(num_extra):
            line = f_in.readline()
        for i in range(50):
            line = f_in.readline()
            f_out.write(line)

except:
    print("Error: ", sys.exc_info())
									

Код (Pascal)

program File31;
var
 S:String;
 F_in,F_out: File of Integer;
 El,i,len:integer;
begin
  Write('File1: ');
  Readln(S);
  Assign(F_in,S);
  Reset(F_in);
  len:=1;
  while not eof(F_in)do
   begin
    Read(F_in,El);
    inc(len);
   end;
  Close(F_in);
  Reset(F_in);
  Assign(F_out,'~'+S);
  ReWrite(F_out);
  i:=1;
  while (not eof(F_in))do
   begin
    Read(F_in,El);
    if i > (len - 50) then Write(F_out,El);
    inc(i);
   end;
  Close(F_in);
  Close(F_out);
  Erase(F_in);
  Rename(F_out,S);
end.
									




File. Абрамян
File 1 Просмотров: 4808
File 2 Просмотров: 4084
File 3 Просмотров: 2023
File 4 Просмотров: 2555
File 5 Просмотров: 1940
File 6 Просмотров: 1696
File 7 Просмотров: 1518
File 8 Просмотров: 2019
File 9 Просмотров: 1196
File 10 Просмотров: 1788
File 11 Просмотров: 1241
File 12 Просмотров: 1396
File 13 Просмотров: 1226
File 14 Просмотров: 1133
File 15 Просмотров: 1015
File 16 Просмотров: 1270
File 17 Просмотров: 1466
File 18 Просмотров: 1025
File 19 Просмотров: 916
File 20 Просмотров: 1089
File 21 Просмотров: 927
File 22 Просмотров: 1154
File 23 Просмотров: 952
File 24 Просмотров: 952
File 25 Просмотров: 1017
File 26 Просмотров: 1003
File 27 Просмотров: 843
File 28 Просмотров: 1119
File 29 Просмотров: 874
File 30 Просмотров: 961
File 31 Просмотров: 896
File 32 Просмотров: 906
File 33 Просмотров: 963
File 34 Просмотров: 983
File 35 Просмотров: 806
File 36 Просмотров: 859
File 37 Просмотров: 745
File 38 Просмотров: 817
File 39 Просмотров: 831
File 40 Просмотров: 788
File 41 Просмотров: 795
File 42 Просмотров: 996
File 43 Просмотров: 739
File 44 Просмотров: 753
File 45 Просмотров: 812
File 46 Просмотров: 713
File 47 Просмотров: 858
File 48 Просмотров: 966
File 49 Просмотров: 853
File 50 Просмотров: 888
File 51 Просмотров: 861
File 52 Просмотров: 888
File 53 Просмотров: 745
File 54 Просмотров: 736
File 55 Просмотров: 788
File 56 Просмотров: 868
File 57 Просмотров: 706
File 58 Просмотров: 825
File 59 Просмотров: 788
File 60 Просмотров: 1275

Комментарии

Чтобы написать комментарии вам нужно войти в систему или зарегистрироваться



Заявка на расчет