-2

For example, I created a directory and want to populate it with a specified number of files. For example

File 1 
File 2
File 3
etc . . .
File n

all in the same directory.

planetp
  • 14,248
  • 20
  • 86
  • 160

3 Answers3

2

If you're running Python 3.4+ you can do it easily with pathlib:

from pathlib import Path

n = 9 
for i in range(1, n+1):
    Path(f"File{i}").touch() 
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
1

Python2/3 compliant, using os.system.

Unix/GNU

import os

for i in range(5):
    os.system('touch File%d' %i)

An alternative approach using subprocess, as suggested by idjaw:

import subprocess
for i in range(5):
    subprocess.Popen(['touch', 'File{}'.format(i)])

Windows

for i in range(5):
    os.system('type nul > File%d' %i)

OS Independent

for i in range(5):
    with open('File%d' %i, 'w') as f: pass
cs95
  • 379,657
  • 97
  • 704
  • 746
  • If you are going to go that route, you are better off using subprocess instead: `subprocess.Popen(['touch', 'file{}'.format(i)])` – idjaw Jun 11 '17 at 22:34
  • @idjaw It's a clean alternative. But why would you prefer one over the other, in this instance? – cs95 Jun 11 '17 at 22:36
  • To your point, absolutely a clean alternative. The `subprocess` module was introduced after `os.system`, therefore is more well supported and I believe supports more if you look to go more *advanced*. I'll point to some answers that discussed this [here](https://stackoverflow.com/questions/4813238/difference-between-subprocess-popen-and-os-system). However, to not take away from your answer, for this example in particular, yes it provides a clean solution. – idjaw Jun 11 '17 at 22:41
  • I never thought I'd see a platform-dependent way of creating a file in _python_, yet here we are. Just... just why? – Aran-Fey Jun 11 '17 at 22:47
  • @Rawing for the purpose of completeness, I suppose? I don't know, it is very late here. – cs95 Jun 11 '17 at 22:48
1

Without pathlib, and cross-platform:

import os

target_path = "/path/to/target/dir"
files_num = 9

for i in range(files_num):
    with open(os.path.join(target_path, "File{}".format(i+1)), "a"):
        pass
zwer
  • 24,943
  • 3
  • 48
  • 66
  • 1
    `"/path/to/target/dir"` is not a very cross-platform path. – planetp Jun 12 '17 at 06:36
  • @planetp - actually, it is (as long as we're not talking about some obscure OSes or fringe Python implementations, of course). Python does automatic path conversions for all file operations, it's just that different OSes will mount different devices/partitions in different ways so Windows would expect to set a drive letter in the beginning if you want to select a different partition/device, but the above path will work perfectly fine for selecting the root path from your CWD be it Windows, Linux, BSD, OSX or any other flair of those. – zwer Jun 12 '17 at 11:53
  • Then why bother with `os.path.join()`? – planetp Jun 12 '17 at 21:04
  • Because while one cannot be responsible for user input (and both `target_path` and `files_num` are obviously user inputs here) that doesn't mean that the meat of a process should not follow best practices. – zwer Jun 12 '17 at 21:28