10 examples of 'python create file if not exists' in Python

Every line of 'python create file if not exists' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
23def create_file(textfile):
24 """ Creates text file containing
25 directory description
26 if the description doesn't exist
27 """
28 try:
29 err_text = '"This directory doesn\'t have description.' +\
30 'Would you like to create one now?"'
31 subprocess.check_call([
32 'zenity',
33 '--error',
34 '--text=' + err_text
35 ])
36 except subprocess.CalledProcessError:
37 sys.exit()
38
39 # ensure we create the file
40 with open(textfile,'w') as text:
41 text.write('')
42
43 try:
44
45 output = subprocess.check_output([
46 'zenity',
47 '--text-info',
48 '--editable',
49 '--filename=' + textfile
50 ])
51 except subprocess.CalledProcessError:
52 sys.exit()
53
54 with open(textfile,'w') as text:
55 text.write(output.decode())
34def create(self, path, contents):
35 dname = os.path.dirname(path)
36 if len(dname) > 0 and not os.path.isdir(dname):
37 os.makedirs(dname)
38
39 with open(path, 'w+') as fd:
40 fd.write(contents)
84def create_script(script, name):
85 """Create script as temp file to be run on Stoomboot"""
86
87 script_name = 'his_{name}.sh'.format(name=name)
88 script_path = os.path.join('/tmp', script_name)
89
90 with open(script_path, 'w') as script_file:
91 script_file.write(script)
92 os.chmod(script_path, 0o774)
93
94 return script_path, script_name
127def create_if_required(self, path):
128 if not os.path.exists(path):
129 os.makedirs(path)
111def ensure_file_exists(filename, context=None):
112 real_filename = filename
113 if context:
114 real_filename = realpath_with_context(filename, context)
115 if not os.path.exists(real_filename):
116 create_textfile_with_contents(real_filename, "")
117 assert os.path.exists(real_filename), "ENSURE file exists: %s" % filename
118def make_accessible(path):
119 os.chmod(os.path.dirname(path), stat.S_IWRITE | stat.S_IEXEC | stat.S_IREAD)
120 os.chmod(path, stat.S_IWRITE | stat.S_IEXEC | stat.S_IREAD)
172def write_file(filename, content=None, mode="w"):
173 """
174 write_file(filename, content=None, mode="w")
175
176 Creates a file including parent directories, default mode mode is 'w'
177 """
178 if not os.path.exists(filename):
179 file_path = os.path.splitdrive(filename)[-1]
180 folders = os.path.split(file_path)[0]
181 mkdir(folders)
182 with open(filename, mode) as myfile:
183 myfile.write(str(content))
63def put_file(dirname, filename):
64 """Put file into directory and return absolute path."""
65 path = os.path.realpath(os.path.join(dirname, filename))
66 mkdir_p(os.path.dirname(path))
67 with open(path, 'w') as stream:
68 stream.write(filename)
69 return path
25def touch(filename, contents=''):
26 path, _ = os.path.split(filename)
27 if len(path) > 0:
28 try:
29 os.makedirs(path)
30 except FileExistsError:
31 pass
32
33 with open(filename, 'w') as fp:
34 fp.write(contents)
108def create_files():
109 f = open("linked.lst", "a");
110 f.close();
111 f = open("linkedsrc.lst", "a");
112 f.close();
113 f = open("skipped.lst", "a");
114 f.close();
115 f = open("skippedsrc.lst", "a");
116 f.close();

Related snippets