How to read files from USB connected phone in Python? - python

I made a Python program that reads all the files in a directory who's path is inputted and then does some things to them and outputs some things into a .txt file.
I used os.walk to read the files in the directory
def dir_read(pathName = str(), fileList=[]): #fileList is output
cur_dir = [i[2] for i in os.walk(pathName)] #current directory
for i in cur_dir:
for j in i:
fileList.append(j)
and then checking for correctly inputted paths like this
fileList=[]
while True:
dir_read(pathName, fileList) #FUNCTION CALL
if not fileList: #if it is empty
print("The path is written incorrectly, is empty or does not exist. Please re-enter now or "
"close the program and enter it inside PATH.txt:")
pathName = str(input())
else:
break #if the list is not empty (the path is entered correctly) the while loop breaks
However if I enter something like This PC\Nokia 6.1\Card SD it keeps giving me the error message. Also I noticed same thing happens with things like Desktop or Documents. But if I enter something like C:\Program Files (x86)\Notepad++ it works perfectly.
I tried replacing \ with / as well and it doesn't work.
My phone doesn't have a specific drive like F: as you can see here: https://imgur.com/a/Educ5RJ
I am using Windows 10.
How do I fix this?

I believe your directory should be like this:
on Linux:
media/This PC/Nokia 6.1/Card SD
on Windows:
You need to know the drive letter you can run this command:
C:\>wmic logicaldisk where drivetype=2 get deviceid, volumename, description
let's say it returned X:
then your directory should be something like this:
X:\Nokia 6.1\Card SD
I'm not sure if you are supposed to remove the Nokia 6.1 or not

try Using subst command and if you have spaces on your device's name put it on quotes
Ex:
subst E: "The Name Goes Here"

Related

How to automatically run python script, when file is added to folder?

How can I automatically run a python script, whenever a Word-File is added to a specific folder? This python script would need to work with the Word-File afterwards. My operating system is Windows.
Thanks for your help in advance.
There are several ways to do this and also a special Python package for this
Watchdog Python script for Windows file system
https://pypi.python.org/pypi/watchdog
https://blog.philippklaus.de/2011/08/use-the-python-module-watchdog-to-monitor-directories-for-changes
I have tried all these solutions, but it didnt worked well for me
But!
I have got the solution,
Heres the code:
path_to_watch = "your/path"
print('Your folder path is"',path,'"')
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
after = dict ([(f, None) for f in os.listdir (path_to_watch)])
added = [f for f in after if not f in before]
if added:
print("Added: ", ", ".join (added))
break
else:
before = after
I have edited the code, the orginal code is available at http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
The original code was made in python 2x so you need to convert it in python 3.
Note:
Whenever you add any file in path, it prints the text and breaks, and if no files are added then it would continue to run.
How can I automatically run a python script, whenever a Word-File is added to a specific folder?
A solution for this can be found in the following code:
import os
# define path for file
word_file = "path/to/file"
while True:
if os.path.exists(word_file):
# your code here
This code uses the os module to interact with the PC's file system, and starts a while loop that always runs. The conditional statement's condition is the existence of the word_file in the specified location, and you can execute your desired code within the if statement.
I hope this is helpful - let me know if you have any questions!
You may want to look into creating a python service.
How do you run a Python script as a service in Windows?
Like the other answer here, you will create an enduring loop to periodically check the filesystem for your file and then do something if/when it finds it.
you can use watchdog, and create your own eventHandler, like, overwriting original functions inside filesystemeventhandler, by creating a class using filesystemeventhandler(class MyHandler(fileSystemEventHandler):), you can change on_any_event or on_created_ or on_modified.
If you want to run this script infinite you can use this one:
import os
path_to_watch = "C:/Users/someone/"
print('Your folder path is"',path_to_watch,'"')
old = os.listdir(path_to_watch)
print(old)
while True:
new = os.listdir(path_to_watch)
if len(new) > len(old):
newfile = list(set(new) - set(old))
print(newfile[0])
old = new
extension = os.path.splitext(path_to_watch + "/" + newfile[0])[1]
if extension == ".zip":
print("hello")
else:
continue
else:
continue
In this case, I was monitoring for a new .zip file. But when you change it into your wanted extension, it will run something when there's a new word document. When you put the script you want to run when there is a new word document under the "if extension..." it should work.
Code is based on the script from #Faraaz Kurawle.

How do I access a file in a sub-directory on user input without having to state the directory in Python 2.7.11?

I have a program that relies on user input to enter files for the program to open in Python 2.7.11. I have all of those files in a sub-directory called TestCases within the original directory Detector, but I can't seem to access the files in TestCases when running the program from the super-directory. I tried to use os.path.join but to of no avail. Here is my code:
import os.path
def __init__(self):
self.file = None
os.path.join('Detector', 'TestCases')
while self.file == None:
self.input = raw_input('What file to open? ')
try:
self.file = open(self.input, 'r')
except:
print "Can't find file."
My terminal when I run the program goes as follows:
>>> What file to open? test.txt # From the TestCases directory
>>> Can't find file.
>>> What file to open? ...
Am I using os.path.join incorrectly? I thought it was supposed to link the two directories so that files could be accessed from the sub-directory while running the program from the super-directory.
You are using os.path.join('Detector', 'TestCases'), that should return 'Detector/TestCases', but you aren't storing that variable anywhere.
I suppose that you are in Detector directory and you want to open files in TestCases. I that case you can use path join (It concatenates its arguments and RETURNS the result):
import os.path
file = None
while not file:
input = raw_input('What file to open? ')
try:
filepath = os.path.join('TestCases', input)
file = open(filepath, 'r')
except IOError:
print "Can't find " + input
I have stored the result of os.path.join so you could see that it doesn't change the directory, it just concatenates its arguments, maybe you was thinking that function will change the directory, you can do it with os.chdir.
Try it first in a simple script or in the terminal, it will save many headaches.
The documentation about os.path.join
Join one or more path components intelligently. The return value is the concatenation of path...
It seems like you expect it to set some kind of PATH variable or affect the current working directory. For a first start it should be sufficient to add something like this to your code:
open(os.path.join("TestCases",self.input), 'r')

Spaces in directory path python

I'm a noob at coding Python and I've run into something that no amount of Googling is helping me with.
I'm trying to write a simple Directory listing tool and I cannot seem to deal with Spaces in the directory name in OSX.
My code is as follows:
def listdir_nohidden(path):
import os
for f in os.listdir(path):
if not f.startswith('.'):
yield f
def MACListDirNoExt():
import os
MACu = PCu = os.environ['USER']
MACDIR = '/Users/'+MACu+'/Desktop//'
while True:
PATH = raw_input("What is the PATH you would like to list?")
if os.path.exists(PATH):
break
else:
print "That PATH cannot be found or does not exist."
NAME = raw_input ("What would you like to name your file?")
DIR = listdir_nohidden(PATH)
DIR = [os.path.splitext(x)[0] for x in DIR]
f = open(''+MACDIR+NAME+'.txt', "w")
for file in DIR:
f.write(str(file) + "\n")
f.close()
print "The file %s.txt has been written to your Desktop" % (NAME)
raw_input ("Press Enter to exit")
For ease of trouble shooting though I think this could essentially be boiled down to:
import os
PATH = raw_input("What is the PATH you would like to list")
os.listdir(PATH)
When supplying a directory path that contains spaces /Volumes/Disk/this is a folder it returns
"No such file or Directory: '/Volumes/Disk/this\\ is\\ a\\ folder/'
It looks like its escaping the escape...?
Check the value returned from raw_input() for occurences of '\\' and replace them with ''.
a = a.replace('\\', '')
I just ran into this, and I'm guessing that what I was hastily doing is also what you were trying. In a way, both #zwol and #trans1st0r are right.
Your boiled down program has nothing wrong with it. I believe that if you put in the input /Volumes/Disk/this is a folder, everything would work fine.
However, what you may have been doing (or at least, what I was doing) is dragging a folder from the Finder to the Terminal. When you drag to the Terminal, the OS automatically escapes spaces for you, so what ends up getting typed into the Terminal is /Volumes/Disk/this\ is\ a\ folder.
So either you can make sure that what you "type in" doesn't have those backslashes, or you can use #trans1st0r's suggestion as a way to support the dragging functionality, though the latter will cause issues in the edge case that your desired path actually has backslashes in it.

Letting user choose a directory to save a file, if directory doesn't exist how to ask for a directory again

I'm writing a script to save a file and giving the option of where to save this file. Now if the directory entered does NOT exist, it breaks the code and prints the Traceback that the directory does not exist. Instead of breaking the code I'd love to have have a solution to tell the user the directory they chose does not exist and ask again where to save the file. I do not want to have this script make a new directory. Here is my code:
myDir = input('Where do you want to write the file?')
myPath = os.path.join(myDir, 'foobar.txt')
try:
f = open(myPath, 'w')
except not os.path.exists(myDir):
print ('That directory or file does not exist, please try again.')
myPath = os.path.join(myDir, 'foobar.txt')
f.seek(0)
f.write(data)
f.close()
This will get you started however I will note that I think you need to tweak the input statement so the user does not have to put quotes in to not get an invalid syntax error
import os
myDir = input("Where do you want to write the file ")
while not os.path.exists(myDir):
print 'invalid path'
myDir = input("Where do you want to write the file ")
else:
print 'hello'
I do not know what your programming background is - mine was SAS before I found Python and some of the constructions are just hard to think through because the approach in Python is so much simpler without having a goto or similar statement but I am still trying to add a goto approach and then someone reminds me about how the while and while not are simpler, easier to read etc.
Good luck
I should add that you really don't need the else statement I put it there to test/verify that my code would work. If you expect to do something like this often then you should put it in a function
def verify_path(some_path_string):
while not os.path.exists(some_path_string):
print 'some warning message'
some_path_string = input('Prompt to get path')
return some_path_string
if _name_ == main:
some_path_string = input("Prompt to get path")
some_path_string = verify_path(some_path_string)

Verify file names by standard

I'm fairly new to python scripting and I want to verify file names in a directory and subdirectory.
The verification should be case sensitive.
I'm using python 2.6.5
OS: win7 and xp
I prompt for the following user input:
prompt = "year"
year = raw_input(prompt)
prompt = "number"
number = raw_input(prompt)
From here I want to search/verify that the following files and folders exist and their filename is correct.
folderstructure:
..\foobar_(number)_version1\music
Files in subfolder 'music'
(year)_foobar_(number)_isnice.txt
(year)_itis(number)hot_today.txt
(year)_anything_is(number)possible.txt
(year)_something_{idont_want_to_check_this_part}_(number)_canbe_anything.txt
Note that all text including underscores are always the same, and thus should always be right, except for the things between () or {}.
I want to output the results to a txt file which reports if the filename is correct or not.
What is the most logical method to archieve this?
I've read the lib documentation fnmatch(.fnmatchcase), RE and os(.path.isfile) and searched here for examples, but I just can't figure out where and how to start.
Can anyone point me in the right direction?
[edit]
As soon as my script has the working basics I'll post my code for reference or to help others.
[edit2] my first non-hello world script
import os
import re
#output :
file_out = "H:\\output.txt"
f_out = open(file_out, 'w')
print "-------start-script----------"
#input
prompt = "enter 4 digit year: "
year = raw_input(prompt)
prompt = "enter 2 digit number: "
number = raw_input(prompt)
print "the chosen year is %s" % (year)
print "the chosen number is %s" % (number)
f_out.write ("start log!\n")
f_out.write ("------------------------------------------\n")
f_out.write ("the chosen year is %s\n" % (year))
f_out.write ("the chosen number is %s\n" % (number))
#part i'm working on
print "end script"
f_out.write ("------------------------------------------\n")
f_out.write ("end script\n")
#close file
f_out.close()
Take a look at the glob module - this will help you get a list of files in the current directory:
import glob
year = raw_input('Year: ') # Example: Year: 2009
number = raw_input('Number: ') # Example: Number: 12
filenames = glob.glob('{year}_*{number}*'.format(year=year, number=number))
Filenames will be anything in the current directory that meets the following criteria:
Begins with 2009_
Any number of characters until it matches 12
Any number of characters following 12.
os.path.exists is a good way to check if the file exists, or os.path.isfile if you want to make sure that it's really a file and not a directory named like a file. For Python3, check these docs, and like the link ghostbust555 mentioned says, be careful of race conditions if you plan on doing anything besides verifying their existence.
Based on your comment, it looks like this is a job for regular expressions. The pseudo code for what you need to write looks something like this:
for filename in list of filenames:
if filename is not valid:
print "<filename> is not valid!"
Aside from the actual pattern, the actual python code could look like this:
import os
import re
pattern = 'Put your actual pattern here'
# For a different directory, change the . to whatever the directory should be
for filename in os.listdir('.'):
if not re.match(pattern, filename):
print("Bad filename: ", filename)
This is not meant to be a full answer, but an extension of #Wayne Werner's answer. I don't have enough reputation points yet to comment. ;0
Wayne's approach using format I think is pointing to what you should do because it's
validating filename BEFORE the files are built instead of after. And it seems that's what you're doing and have control over?
I would do as much validation at the user input level as possible.
Validate the other parts from whereever you get them.
Build a dictionary with the parts.
Build your file_name.
For example, at the user input level, something like:
yourDict = dict()
year_input = raw_input('What is the year'?)
if not year_input.isdigit():
year_input = raw_input('Only digits please in the format YYYY, example: 2012'):
yourDict[year] = year_input
Then continute to add key:values to yourDict by validating the other values by whatever criteria it is you have. (Use re module or other method's mentioned).
Then, as Wayne was doing, use .format() with a passed in dictionary to map to the correct parts.
format1 = "{year}{part1}{number}{part2}.txt".format(**yourDict)
The approach also allows you to quickly build new formats with the same parts, and you can pick and choose what keys in the dictionary you need or don't need for each format.
Hope that's helpful.
import os.path
year = 2009
file1 = year + "_foobar_" + number + "_isnice.txt"
os.path.exists(file1)

Categories