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)


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


Download 6.05 Kb.
bet2/2
Sana19.09.2023
Hajmi6.05 Kb.
#1681563
1   2
Bog'liq
File 1 s qator berilgan. Agar s yaroqli fayl nomi bo’lsa, u hold-fayllar.org

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)




http://fayllar.org
Download 6.05 Kb.

Do'stlaringiz bilan baham:
1   2




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