Attribute Error 'module' object has no attribute 'ascii_letters' - python

Why do I receive the error message in the title from the code below?
Edit: Cause I didn't pay attention to how I wrote "ascii". Thanks everyone
The code below works fine on my Iphone IDE but not on my Windows 7 (w/Notepad++ and Command Prompt). I checked the directory to see if any string.py files existed which I did not see any. I ran a search on my desktop and found 4 files named that, two of which said they were complied. I deleted the compiled files and left the other two. I'm a noob.
import string
import random
x = string.acsii_letters
y = random.choice(x)
print y * 5

It should be string.ascii_letters letters instead of string.acsii_letters. If that's a typo in code statement here only, then your guess must be right, there is another string module in your PYTHONPATH. Open python shell,
import string
print(string.__file__)
to ensure string is being imported from right path. If its not remove that path from PYTHONPATH.

In python 3 I found that using the string.ascii_letters works as string.letters results in an AttributeError.

You have a typo. It should be string.ascii_letters or string.letters. You can look at the attributes of the string module with dir(string) and see what you can access.

i had the same issue the reason was that the name of the file is the same of the module name . so just rename your file the module will work well

Related

How to use 7z from the python scripts

I have following syntax in my python script.
x = Popen("\"C:/Program Files/7-zip/7z\" a -tzip " + new_file + " general/*")
Can anyone please explain the meaning of this syntax?
From some forums, I am explaining my understanding.
First the 7z application will be started from the command line interface from the mentioned path. Then the folder " general" in the current directory will be copied into new_file. Am I right?
Please note, I am totally new to Python. Hence please excuse my understanding.
When I run this command, I am getting following Warning. Can anyone please let me know the issue in the syntax?
7-Zip 17.01 beta (x64) : Copyright (c) 1999-2017 Igor Pavlov : 2017-08-28
Open archive: <new_file>.zip
Path = <new_file>.zip
Type = zip
Physical Size = 93678166
Scanning the drive:
WARNING: The system cannot find the file specified.
general
Writing the path like this: "<path>" is valid? What is the difference between "" and "<path>"? Frankly speaking, I have copied this from some forum for my application.
What does " general/* " means? I know that if file name to be written with the space, it should be quoted with "". But what does it mean with wildcard characters " general/*"?
Please note, this scripts it going to be executed with Windows command line interface.
Thanks in advance.
When I run this command, I am getting following Warning. Can anyone please let me know the issue in the syntax?
WARNING: The system cannot find the file specified.
general
The reason for this warning is not in the syntax; it's because there is no folder named general.
Writing the path like this: "<path>" is valid?
It seems so, but you probably don't really want to have the file name contain less-than or greater-than characters (angle brackets). Presumably where you've copied this from there's a line
new_file = "<new_file>"
- you were supposed to replace <new_file> with a file name of your choice, but you didn't.
What does " general/* " means?
In this context it means that all files in the folder general are to be added to the zip file.

Python(on Win10): Path issue replacing "\" with "/"

Preface: I am a beginner to Python
Problem statement: I am writing a script wherein I will be launching an application (Gotit.exe) sitting at particular path lets say D:\Some Folder\SomeMore Folder\AgainFolder\myPythonFolder\Gotit.exe. I have kept the python-script also in myPythonFolder.
I am accessing the folder path via os.path.dirname(os.path.realpath(__file__)) and selecting particular application by appending it with \Gotit.exe but when passing the same appended string stored in a variable i.e. GotitexePath to os.system(GotitexePath) its throwing error as,
'D:\Some ' is not recognized as an internal or external command,
operable program or batch file.**
Kindly help me out to solve the said issue
I am using python 3.8.2 on Win10 Machine
The error is pointing to Some Folder name. Since there is a space in path you provide, the system doesn't know whether it is a part of folder name or it is a next argument to the command.
You need to escape the blank space. There are multiple ways to to it. For example wrap the path with double quotes:
"D:\Some Folder\SomeMore Folder\AgainFolder\myPythonFolder\Gotit.exe"
For more ways see this post
os.system("\"%s\"" % GotitexePath)
A the previous replies say, you need to add additional quotation marks around the path for the windows command line.

python import from string

I'm trying to import another python script with a name of the form fe_fi_fo_fam.py, I'm using importlib.import_module("fe_fi_fo_fam.py") but I'm getting the error : "module fe not found" , how do I get it to read the whole string instead of only the first part? I'm new to python and I seem to have scoured the internet without an answer.
Yo can instead use the built-in function exec after you compose the module string
>>> modOs = 'os'
>>> exec('import ' + modOs)
>>> os.getcwd()
'C:\\Desktop'
Note: Python 3.6 exec
I think you have to understand the difference between module name in Python and file name. importlib.import_module() gets a "module name", not a "file name", so "fe_fi_fo_fam.py" is not a valid argument, instead you have to use "fe_fi_fo_fam" with no .py extension.

Playing Audio with subprocess.call in Python

I wanted to play a .wav file, without using external modules, and i read i could do that using this:
def play(audio_file_path):
subprocess.call(["ffplay", "-nodisp", "-autoexit", /Users/me/Downloads/sample.wav])
I however get:
SyntaxError: invalid syntax
If i use os.path.realpath to get the absolute path of the file, i get just the same thing. (The path i see at get info)
Environment is OSX, Python 2.7
Can someone tell me what i am doing wrong? I am new to Python (and to Programming).
There are multiple problems.
Indentation
Code inside the function should be indented, to show that it is part of the function
File name should be in a quotes
It should be a string
It should be:
def play(audio_file_path):
subprocess.call(["ffplay", "-nodisp", "-autoexit", "/Users/me/Downloads/sample.wav"])

os.listdir can't see my directory

I am working on a python script that installs an 802.1x certificate on a Windows 8.1 machine. This script works fine on Windows 8 and Windows XP (haven't tried it on other machines).
I have isolated the issue. It has to do with clearing out the folder
"C:\Windows\system32\config\systemprofile\AppData\LocalLow\Microsoft\CryptURLCache\Content"
The problem is that I am using the module os and the command listdir on this folder to delete each file in it. However, listdir errors, saying the folder does not exist, when it does indeed exist.
The issue seems to be that os.listdir cannot see the LocalLow folder. If I make a two line script:
import os
os.listdir("C:\Windows\System32\config\systemprofile\AppData")
It shows the following result:
['Local', 'Roaming']
As you can see, LocalLow is missing.
I thought it might be a permissions issue, but I am having serious trouble figuring out what a next step might be. I am running the process as an administrator from the command line, and it simply doesn't see the folder.
Thanks in advance!
Edit: changing the string to r"C:\Windows\System32\config\systemprofile\AppData", "C:\Windows\System32\config\systemprofile\AppData", or C:/Windows/System32/config/systemprofile/AppData" all produce identical results
Edit: Another unusual wrinkle in this issue: If I manually create a new directory in that location I am unable to see it through os.listdir either. In addition, I cannot browse to the LocalLow or my New Folder through the "Save As.." command in Notepad++
I'm starting to think this is a bug in Windows 8.1 preview.
I encountered this issue recently.
I found it's caused by Windows file system redirector
and you can check out following python snippet
import ctypes
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
#Example usage
import os
path = 'C:\\Windows\\System32\\config\\systemprofile\\AppData'
print os.listdir(path)
with disable_file_system_redirection():
print os.listdir(path)
print os.listdir(path)
ref : http://code.activestate.com/recipes/578035-disable-file-system-redirector/
You must have escape sequences in your path. You should use a raw string for file/directory paths:
# By putting the 'r' at the start, I make this string a raw string
# Raw strings do not process escape sequences
r"C:\path\to\file"
or put the slashes the other way:
"C:/path/to/file"
or escape the slashes:
# You probably won't want this method because it makes your paths huge
# I just listed it because it *does* work
"C:\\path\\to\\file"
I'm curious as to how you are able to list the contents with those two lines. You are using escape sequences \W, \S, \c, \s, \A in your code. Try escaping the back slash like this:
import os
os.listdir('C:\\Windows\\System32\\config\\systemprofile\\AppData')

Categories