0

I am trying to use a script to open a file inside a network drive using python. The script is given below:

import os
import subprocess

file_path = r"O:\XXXX\test.xls"
subprocess.Popen(filepath, shell=True)

The network drive requires sign in but I always by default sign it the moment I on the computer. Also, using a os.listdir(folderpath) has no problem going into the network drive and listing all the files in the directory containing the file.

Tried some suggestions from similar posts but they don't work.

I am using Python 2.7, and Windows.

UPDATE:

No error was prompted after executing the script.

I am trying to open an Excel file. The script works to open Excel in other folders within the computer, but just not within the network drive.

Jake
  • 2,482
  • 7
  • 27
  • 51
  • What is the error you're getting? – Ishay Peled Jul 07 '16 at 08:44
  • Just updated the post. There was no error prompted. Thanks! – Jake Jul 07 '16 at 08:48
  • You should not open arbitrary files with `Popen`. `Popen` should be used to execute executables. – DeepSpace Jul 07 '16 at 08:54
  • 1
    Okay I think I managed to understand ops problem - you want the file to open in excel? Is that right? Please don't keep us guessing... – Ishay Peled Jul 07 '16 at 08:58
  • Yes, sorry about being unclear. Can you advise how to open it if not by Popen? – Jake Jul 07 '16 at 09:15
  • 1
    do you want `os.startfile(filepath)`? – jfs Jul 07 '16 at 23:16
  • related: [How can I open files in external programs in Python?](http://stackoverflow.com/q/15054434/4279) – jfs Jul 07 '16 at 23:18
  • @J.F.Sebastian your suggestion work! Can you post it as an answer so I can accept it? – Jake Jul 08 '16 at 01:56
  • It doesn't answer the question (why the behavior for the network drive and other folders is different *if it is indeed the case). If `os.startfile()` works for you; you could post it as an answer. [It is encouraged](http://stackoverflow.com/help/self-answer) – jfs Jul 08 '16 at 02:07
  • Yea that's true, but at least it works for my work now. Appreciate it! – Jake Jul 08 '16 at 02:24

2 Answers2

1

Thanks to @J.F. Sebastian's suggestion. replacing subprocess.Popen(filepath, shell=True) with os.startfile(filepath) works.

Jake
  • 2,482
  • 7
  • 27
  • 51
0

I think this could help you

import subprocess

file_path = r"X:\dir\excelfile.xlsx"
#~ also this works
#~ file_path = r"\\server\dir\excelfile.xlsx"
subprocess.call(file_path,shell = True)
  • Hi, this doesn't work too. J.F Sebastian's one using os.startfile(filepath) works though. Thanks anyway! – Jake Jul 08 '16 at 01:56