Now I have a folder(test
) in a network and I want to read all files in this folder, ip is 11.110.100.1
, and I can find all files by this path:
file_path = r"\\11.110.100.1\\test\\"
for file_name in os.listdir(file_path):
print(file_name)
However, I am confused about this file_path
, I mean, it always be true like,
'C:/temp/parts'
or
r'C:\temp\parts'
in Python, I haven't found this format of path, moreover, sometimes it shows error in this path, but when I restart my code, it can be run successfully, why and how to solve this problem? Besides, when I change this file_path to
file_path = '/11.110.100.1/test/'
it shows FileNotFoundError, can not find specific path
CodePudding user response:
On most operating systems, file paths look like this:
/directory/directory/file_name
On Windows, they look like this:
drive:\directory\directory\file_name
There's no good reason other than some ancient history (to do with DOS 1.0 and 2.0). However, we're stuck with that design choice for the time being and it causes (understandable) confusion with developers and users alike. You can thank Paul Allen from Microsoft for this, or perhaps some suit at IBM at the time, or perhaps even designers of Tops-10 or CP/M - in the end, it doesn't really matter, we're stuck with it for backward compatibility.
Because in most operating systems, \
is an escape character (i.e. you can use it to insert special characters like a newline \n
), Python follows that convention. That explains why you need the r
in front of a Windows-format file path (e.g. r'drive:\directory\directory\file_name'
), telling Python not to interpret the backslash as an escape character. Otherwise, you'd have to double up on the backslashes like 'drive:\\directory\\directory\\file_name'
.
However, because Windows now understands forward slashed in many places as well, and because many libraries in Python need to work across multiple operating systems, in almost all cases you can just use forward slashes in file paths instead, so 'drive:/directory/directory/file_name'
also works.
And because a path can be relative (on any OS), it doesn't have to start with a drive. Although it will translate to an absolute path that starts with either a drive or a network location like \\server\share\
(adding more complication because that needs a double backsslash, so a quadruple one in a regular Python string). But if a path starts with /
or \
, it is interpreted to mean "from the root of the current drive" on Windows, or "from the root of the file system" on other OSes.
So, '/11.110.100.1/test/'
means "a folder called 'test' in a folder called '11.110.100.1' in the root of the current drive, or the root of the file system".
That's probably not what you mean, your original r"\\11.110.100.1\\test\\"
works because it has the r
(so the backslashes stay doubled up), which means the first part is interpreted as a server location \\11.110.100.1
and the rest is the path of the resource on that server \\test\\
- most operating systems just ignore the extra backslashes if they separate folders.
Something like x\\y\\\\\z
just means the same as x\y\z
, because x\\y
just amounts to x\.\y
.
So, the path in your \\test\\
is just test
and your string could have been r"\\11.110.100.1\test\"
or "\\\\11.110.100.1\\test\\"
and it would have been exactly the same.
If you want it to work with forward slashes, in most cases this would likely work: "//11.110.100.1/test/"
since that's what it would look like on any OS other than Windows.
Try:
file_path = '//11.110.100.1/test/'
for file_name in os.listdir(file_path):
print(file_name)
(single quotes being the default, but your double ones work as well)