Python da Dosya İşlemleri
Merhaba arkadaşlar bu makalemizde Pythonda dosya işlemlerine örnek vereceğiz. İlk önce os kütüphanesini projemize ekliyoruz.

Şekil 1
Bu örneğimizde proje dosyalarımızın olduğu klasörde Files isimli bir klasörün olup olmadığını kontrol ediyoruz. Eğer dosya yoksa ilk önce Files dosyasını oluşturuyoruz.

Şekil 2
Daha sonra oluşturduğumuz dosyanın ismini New Files olarak değiştiriyoruz.

Şekil 3
Oluşturduğumuz klasörü rmdir komutunu kullanarak silebiliriz.
Son olarak oluşturduğumuz New File klasörünün içersine Read me isminde bir text dosyası oluşturuyoruz. Oluşturduğumuz text dosyasının içerisine Hello World yazdırıyoruz.

Şekil 4
import os
# The name and path of the folder to be created in the workspace are defined
fileName = "Files"
path = os.path.join(os.getcwd(), fileName)
try:
# Checking if the folder exists
if not os.path.exists(path):
# Create folder
os.mkdir(path)
print(f"{fileName} The folder has been created.")
else:
print(f"{fileName} The folder already exists.")
# Changing the directory name with rename
newfileName = "New Files"
os.rename(fileName, "New Files")
print(f"The directory named {fileName} It was changed to {newfileName}")
# Delete directory with rmdir
#os.rmdir(newfileName)
print(f"The directory named {newfileName} has been deleted.")
except Exception as e:
print(f"An error occurred: {e}")
# Create txt file
file = open(f"{newfileName}/Read Me.txt", "w")
file.write("Hello World!")
file.close()
file = open(f"{newfileName}/Read Me.txt")
print("")
print(file.read())
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN