0

I use numpy's genfromtxt function for work all the time. Tomorrow I have to make a presentation on how to use Python (something I've only just started learning 6 weeks ago). The problem is that a lot of my audience will be using Mac's or Linux systems. I however, use Windows. I have found the Path function, but it doesn't quite work:

from pathlib import Path

Path('File_180620_123733_Aunp_10nm_stock.txt').resolve() 

which returns:

C:\Users\brand\Downloads\Python\My Files\aupnipam_scan41_3DFLR(1).txt

for windows, which again is my operating system, I need the format of my file path to be

C:\\Users\\brand\\Downloads\\Python\\My Files\\aupnipam_scan41_3DFLR(1).txt

for genfromtxt to work.

Any suggestions to how to make this work, not only for Windows, but for all operating systems?

B. Moore
  • 109
  • 1
  • 11

1 Answers1

0

Your princess is in another castle.

Use os.path.abspath('File_180620_123733_Aunp_10nm_stock.txt'), assuming the file is in your current working directory. Pathlib is primarily intended for more complex path manipulation.

Since different operating systems have different path name conventions, there are several versions of this module in the standard library. The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. However, you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats. They all have the same interface.

https://docs.python.org/3/library/os.path.html#module-os.path

noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67
  • I used the following code, and the path it outputs is the same as in my original question. Is my syntax correct? Did I import os.path correctly? 'import os.path' 'parent = os.path.abspath('File_180620_123733_Aunp_10nm_stock.txt')' 'print(parent)' – B. Moore Jul 13 '18 at 02:03
  • @B. Moore try this https://gist.githubusercontent.com/ageitgey/cc0b42a6bb5ae8adca93ee71d1837056/raw/9d2273d0b1b2f2321651ce393f9fcb90ed102540/my_program.py – noɥʇʎԀʎzɐɹƆ Jul 13 '18 at 14:56