Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating new file in python causes FileNotFoundError

Im trying to create a random set of files from a list of file names in python 3.

#!/usr/local/bin/python3

import random
import sys

random.seed()

print("Reading lines from ", sys.argv[1])

linecount = 0
lines = []
with open(sys.argv[1]) as f:
    for line in f:
         lines.append(line)

filelist = random.sample(lines, 5);
for newfile in filelist:
    print("Touching file ", newfile)
    open(newfile.rstrip(), "a+")

This always fails for the first file with:

$ : ./file-gen.py files.txt
Reading lines from  files.txt
Touching file  62/6226320DE.pdf

Traceback (most recent call last):
  File "./file-gen.py", line 19, in <module>
    open(newfile.rstrip(), "a+");
FileNotFoundError: [Errno 2] No such file or directory: '62/6226320DE.pdf'

Any ideas what's missing?

like image 687
BetaRide Avatar asked Sep 02 '15 13:09

BetaRide


People also ask

How do I fix Python FileNotFoundError?

The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.

Why am I getting a FileNotFoundError?

What is filenotfounderror. It is a system message that the compiler throws when you are trying to execute a command that requires a file that the system cannot find. It can be for various reasons like – wrong file path specified, the file is present in another directory, or an extension error.

How do I fix FileNotFoundError No such file or directory?

The Python "FileNotFoundError: [Errno 2] No such file or directory" occurs when we try to open a file that doesn't exist in the specified location. To solve the error, move the file to the directory where the Python script is located if using a local path, or use an absolute path.

Why am I getting a No such file or directory?

log No such file or directory” the problem is most likely on the client side. In most cases, this simply indicates that the file or folder specified was a top-level item selected in the backup schedule and it did not exist at the time the backup ran.


1 Answers

I believe the problem is that the file mode a+ will create the file if its not present, but not the directory. You would have to write custom code to split the line of the filename from the path (or better still use os.path : https://docs.python.org/2/library/os.path.html, perhaps even os.path.dirname(path)

Have a look at: How to check if a directory exists and create it if necessary?

Be wary of security considerations of creating random paths in your system. Validating that the paths are inside a particular sandbox (consider someone putting an entry in your file of ../../ or /etc/passwd to get you to append random user data.. os.path.abspath can be useful - for this reason I am wary of pasting code which will just create random directories that you copy and paste in without considering that effect.

like image 63
Jmons Avatar answered Sep 26 '22 14:09

Jmons