Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ba98e4

Browse files
author
neotycoder
committed
Adding python script: CreateDirectory.py
Simple script to show how concatenation, creating a directory, and opening and closing a file for writing.
0 parents  commit 7ba98e4

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

createDirectory.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python
2+
3+
# Author: Ty Lim
4+
# Date: 2/4/2015
5+
# File name: createDirectory.py
6+
# Description: This is a simple example of how to check to see if a directory exist, and if not, create it.
7+
# This script also shows an example of how to create a file name with a time stamp, use that filen name
8+
# to open a file for writing, and write to the file, and finally, close it.
9+
10+
# This is nothing too difficult, and you can certainly add more functionality to it if you wish.
11+
# One idea is to build this whole example into an object with subsequent methods.
12+
13+
import os, datetime
14+
15+
# Create the file name
16+
baseFileName = "12345"
17+
app="someAppName"
18+
dateTimeStamp = datetime.date.today()
19+
# This directory needs to exists. It is the parent directory that will house the datafiles.
20+
targetDirectory = "/tmp/datafiles"
21+
targetfileName = str(baseFileName)+"_"+app+"."+str(dateTimeStamp)+".txt"
22+
fullPathToFile = targetDirectory+"/"+targetfileName
23+
24+
print("FileName: " + fullPathToFile)
25+
26+
# Check base path. If it does not exist, create it.
27+
if not os.path.isdir(targetDirectory):
28+
os.makedirs(targetDirectory)
29+
30+
file = open(fullPathToFile,'w')
31+
file.write("This is a test.")
32+
33+
34+
file.close()
35+
36+
37+
38+
39+
40+
41+
42+
43+

0 commit comments

Comments
 (0)