0

I read all the files in the directory with the following lines of code:

os.chdir(path)

extension = 'xml'

all_filenames = [i for i in glob('*.{}'.format(extension))]

Then if I try to parse a file with the name starting with _ I get the following error:

FileNotFoundError: [Errno 2] No such file or directory: '_a-0000e3c4-17e8-8000-9ba2-011c48011c48_6736348.xml'

The Code for parsing the files:

import xml.etree.ElementTree as et
tree = et.parse(all_filenames[252])
root = tree.getroot()

print(f"name: {root[1][0].text}")
print(f"UUID: {root[1][1].text}")

In the example the index [252] corresponds to a random file in the directory that starts with a _

Does anyone knows how can I read files starting with _?

Thanks for the help


UPDATE

Also this does not work:

with open(all_filenames[252]) as file:
    data = file.read() 

I am running python in a windows environment.

Python 3.7.4 installed with anaconda distribution

Windows 10 (1903)


UPDATE 2

Problem might be related to windows, running this code in cmd will also fail:

REN "_undescorearchive.xml" "undescorearchive.xml"
Jorge
  • 1
  • 1
  • Are files with names **not** beginning with `_` parsed correctly? – Błotosmętek Feb 24 '20 at 11:04
  • Yes. Also I can not open files for normal reading if they start with _ – Jorge Feb 24 '20 at 11:08
  • Exact Python and Windows versions please. – Jongware Feb 24 '20 at 11:11
  • Sorry, Python 3.7.4 installed with anaconda distribution and Windows 10 – Jorge Feb 24 '20 at 11:12
  • @Jorge May i see the output of `print(path)` and `print(os.getcwd())` just before `et.parse(...` – stovfl Feb 24 '20 at 11:59
  • I can not tell you the path as it may contain sensitive information, but both prints output the same path. I think I figured the problem and it may be related with how windows work with files starting with underscore (See Update 2) – Jorge Feb 24 '20 at 12:07
  • @Jorge Relevant: [how-do-i-check-if-a-given-string-is-a-legal-valid-file-name-under-windows](https://stackoverflow.com/questions/62771) but `_` is not listed there. – stovfl Feb 24 '20 at 18:26

0 Answers0