File 1 s qator berilgan. Agar s yaroqli fayl nomi bo’lsa, u holda shu nom bilan bo’sh fayl yarating va true deb chop eting. Agar s nomli fayl yaratilmasa,false deb chop eting. Kod (Python)


Download 27.6 Kb.
Sana19.04.2023
Hajmi27.6 Kb.
#1362060
Bog'liq
File 1


File 1
S qator berilgan.Agar S yaroqli fayl nomi bo’lsa , u holda shu nom bilan bo’sh fayl yarating va TRUE deb chop eting.Agar S nomli fayl yaratilmasa ,FALSE deb chop eting.

Kod  (Python)



  1. file_name = input('Fayl nomini kiriting: ')


  2. try:

  3. f = open(file_name, "w")

  4. try:

  5. f.write('')

  6. print('TRUE')

  7. finally:

  8. f.close()

  9. except IOError:

  10. print('FALSE')



File 2
Fayl nomi va N(>1) butun soni berilgan.Berilgan nomli butun sonlardan iborat fayl yarating va unga birinchi N musbat juft sonni (2,4,6 …) yozing.
Kod  (C/C++)
#include


  1. using namespace std;


  2. int main()

  3. {

  4. ifstream fin("input.txt");

  5. ofstream fout("output.txt");

  6. int n;

  7. fin >> n;

  8. for(int i = 2; i <= n * 2; i+=2){

  9. fout << i << " ";

  10. }

  11. return 0;

  12. }

Kod (Python)



  1. import random


  2. file_name = "file02.txt"

  3. N = random.randrange(1,10)

  4. try:

  5. f = open(file_name, "w")

  6. f.write("N = " + (str)(N) + '\n')

  7. try:

  8. for i in range(N):

  9. f.write((str)((i+1)*2) + '\n')

  10. finally:

  11. f.close()

  12. except IOError:

  13. print('Fayl yaratilmadi')


File 3
Fayl nomi va haqiqiy sonlar A va D berilgan.Berilgan nomli haqiqiy sonlar faylini tuzing va unga arifmetik progressiyaning dastlabki 10 a’zosini A boshlang’ich a’zosi va D farqi bilan yozing:A,A+D, A + 2·D, A + 3·D, ...
Kod (Python)

  1. import random


  2. N = random.randrange(1,10)

  3. A = random.randrange(-10,10)

  4. D = round(random.uniform(-5,5),1)

  5. print("N = ", N)

  6. print("A = ", A)

  7. print("D = ", D)


  8. a = [A + i*D for i in range(N)]

  9. print(a)


  10. file_name = "file03.txt"

  11. try:

  12. f = open(file_name, "w")

  13. f.write("N = " + (str)(N) + '; ')

  14. f.write("A = " + (str)(A) + '; ')

  15. f.write("D = " + (str)(D) + '\n')

  16. f.write(str(a))

  17. except IOError:

  18. print('Fayl ishlamadi')

  19. finally:

  20. f.close()


File 4
To’rtta fayl nomi berilgan.Joriy katalogdagi berilgan nomli fayllar sonini toping
Kod (Python)

  1. from os import listdir

  2. from os.path import isfile, join


  3. my_files = ['file01.py', 'file02.py', 'file022.txt', 'file03.py', 'file03.txt', 'file44.py']

  4. print("Искомые файлы:")

  5. print(my_files)


  6. mypath = "."

  7. onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

  8. print("Имеющиеся файлы:")

  9. print(onlyfiles)


  10. set1 = set(my_files).intersection(onlyfiles)

  11. print("Найденные файлы:")

  12. print(set1)

  13. print("Количество найденных файлов:", len(set1))



File 5
Butun sonlar faylining nomi berilgan.Berilgan fayldagi elementlar sonini toping .Agar bir xil nomdagi fayl bo’lmasa -1 ni chop eting.

Kod  (Python)



  1. N = 0

  2. try:

  3. with open('integers.txt','r') as f:

  4. for line in f:

  5. N += 1

  6. print(N,"=>",int(line))

  7. except IOError:

  8. print("Fayl topilmadi ")

  9. print("Elementlar miqdori : ", N)




File 6
K butun soni va manfiy bo’lmagan butun sonlarni o’z ichiga olgan fayl berilgan.Faylning K-elementini chop eting (elementlar 1 dan raqamlangan ).Agar bunday element bo’lmasa , -1 ni chop eting.
Kod (Python)

  1. import random


  2. K = random.randrange(1,11)

  3. print("K:",K)

  4. N = 0

  5. try:

  6. with open('integers.txt','r') as f:

  7. for line in f:

  8. N += 1

  9. print(N,"=>",int(line))

  10. if N == K:

  11. break

  12. except IOError:

  13. print("Файл не найден")


  14. if N == K:

  15. print("Result:",K,"=>",int(line))

  16. else:

  17. print(-1)



Download 27.6 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling