Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Python useful snippets

Posted on February 28, 2018August 4, 2020 by Habibie

In this post you can find useful and frequently used Python snippets. Just copy and paste it!

Creating new directory (folder)

import os

newpath = r'C:\mydirectory\newfolder' 
if not os.path.exists(newpath):
	os.makedirs(newpath)
	print("Folder created.")
else:
	print("Folder already exists.")

Writing to a file

myfile = open("hello.txt", "w")
myfile.write( "Hello world!")
myfile.close()

Create a new folder and write a file inside it

import os
myfolder = r'C:\newfolder' 
os.makedirs(myfolder)
myfile = open(myfolder + "\hello.txt", "w")
myfile.write( "Hello world!")
myfile.close()

How to write a long multi line text into a file

longstring = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit, 

sed do eiusmod tempor incididunt 
ut labore et dolore magna 
aliqua. Ut enim ad minim veniam, quis nostrud ...

exercitation ullamco laboris 
nisi ut aliquip ex ea commodo consequat. 

...

Duis aute irure dolor 
in reprehenderit 
................
in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
...
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
"""

myfile = open("hello.txt", "w")
myfile.write(longstring)
myfile.close()

Creating a list and loop through it

numbers = ["one", "two", "three"]
i = 0
while i < len(numbers):
    print(numbers[i])
    i += 1

Creating a nested list and loop through it again

projects = [
	[ 1, "One" ], 
	[ 2, "Two" ], 
	[ 3, "Three" ],
	[ 4, "Four" ],
	[ 5, "Five" ],
]

i = 0
while i < len(projects):
	for prop in projects[i]:
		print(prop)
	i += 1
Post Views: 353
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme